Check if worksheet exists in another open workbook

My process so far checks to see if another workbooks is open and it if is not, then it opens.  Ok, so far so good.  However, next I need to check in this workbook if the worksheet "DataSource" exists and if not, create it.  I can't get pass the checking for the worksheet.   It errors out at the If Len(.worksheets("DataSource).Name > 0 Then statement.
'First check to see if the file is open and if not, then open.  Then check to see if the
'worksheet DataSource exists in the workbook and if not, create it.  Then query the
'ActiveMasterTempate DataSource worksheet to bring in the Rating data
Dim wks As Worksheet
Dim strColumnHeaders()  As String
strFileExportPathSumm = ThisWorkbook.Worksheets("Settings").Range("FOLDER_PATH_SUMMARY") ' Summary Reports/General Folder, Range name on Settings worksheet, Settings!$I$5

    If Not fntIsWbkOpen("TEST_ClientActiveMasterTemplate.xls") Then
       Application.Workbooks.Open strFileExportPathSumm & "TEST_ClientActiveMasterTemplate.xls", 2
    End If
        With Workbooks("TEST_ClientActiveMasterTemplate.xls")
            If Len(.Worksheets("DataSource").Name) > 0 Then 'Subscrpt out of range nere
                MsgBox "Yes"
                MsgBox "No"
            End If
        End With

Open in new window

Sandra SmithRetiredAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zorvek (Kevin Jones)ConsultantCommented:
There is no direct way to determine if a worksheet exists without either using error handling or a loop. Each of these two techniques is presented below.

The code below uses error handling to determine if a worksheet exists.

[Begin Code Segment]

Public Function IsExistingSheet( _
      ByVal SheetName As String _
   ) As Boolean

   ' Return True if the named sheet exists, False otherwise.

   Dim Worksheet As Worksheet

   On Error Resume Next
   Set Worksheet = Sheets(SheetName)
   On Error GoTo 0
   If Not sheet Is Nothing Then IsExistingSheet = True

End Function

[End Code Segment]

The code below uses a loop to determine if a sheet exists.

[Begin Code Segment]

Public Function IsExistingSheet( _
      ByVal SheetName As String _
   ) As Boolean

' Return True if the named sheet exists, False otherwise.
   Dim TargetSheet As Object
   For Each TargetSheet In ThisWorkbook.Sheets
      If UCase(TargetSheet.Name) = UCase(SheetName) Then
         DoesSheetExist = True
         Exit For
      End If
   Next TargetSheet
End Function

[End Code Segment]


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sandra SmithRetiredAuthor Commented:
That answered that quite nicely.  Thank you.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.