• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

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")
            .Activate
            If Len(.Worksheets("DataSource").Name) > 0 Then 'Subscrpt out of range nere
                MsgBox "Yes"
            Else
                MsgBox "No"
            End If
        End With

Open in new window

0
Sandra Smith
Asked:
Sandra Smith
1 Solution
 
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]

Kevin
0
 
Sandra SmithRetiredAuthor Commented:
That answered that quite nicely.  Thank you.

Sandra
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now