Check if worksheet exists
Find bottlenecks in your Excel workbooks
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

Thread: Check if worksheet exists

  1. #1
    New Member
    Join Date
    Apr 2002
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    Trying to add VBA code to check if a worksheet ("Data") exists in the workbook. Have tried If Not SheetExists("Data") Then and other configurations, but none seem to work.

    Appreciate any suggestions.

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Posts
    363
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Try this:

    Sub findsheet()
    sheetfound = False
    For Each sht In Worksheets
    If sht.Name = "Data" Then
    sheetfound = True
    Exit For
    End If
    Next
    End Sub

    It's never too late to learn something new.

    Ricky

  3. #3
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Another way besides Rickys



    Dim sh As Worksheet

    On Error Resume Next
    Set sh = ThisWorkbook.Sheets("Sheet1")
    If sh Is Nothing Then MsgBox "Sheet doesn't exist!"
    Set sh = Nothing: On Error GoTo 0

    '// Rest of your code
    '// goes here


    Kind Regards,
    Ivan F Moala From the City of Sails

  4. #4
    New Member
    Join Date
    Apr 2002
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks, Ricky and Ivan. Both suggestions work great.

    Appreciate the quick response.


  5. #5
    New Member
    Join Date
    Apr 2002
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Ricky,

    I tried to add an ElseIf in case the "Data" sheet is not found, but condition fails. Also tried to change condition to <> "Data" but that also failed. Any suggestions on how to add an Elseif the sht.name is not "Data"?

    Thanks for any assistance.

  6. #6
    New Member
    Join Date
    Apr 2002
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Ricky,

    Apologies for previous message. Discovered I have to add the If condition after Next and test if Sheetfound = True or False.

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com