Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does Sheet Exist (03)

    What is the best way to test if a sheet exists in a workbook?

    Thanks,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Does Sheet Exist (03)

    Open the workbook and look at it?

    Seriously, you don't specify the context of your question - do you mean in VBA? And what do you know about the sheet? Its name?

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does Sheet Exist (03)

    Hans,

    Sorry for being vague. I know the sheet name. Basically I would use VBA to open the file and look for the existance of a sheet in the file.

    Regards,
    John

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Does Sheet Exist (03)

    You can put this function in a module:

    Public Function SheetExists(SheetName As String) As Boolean
    Dim wsh As Worksheet
    On Error Resume Next
    Set wsh = Worksheets(SheetName)
    SheetExists = (Err = 0)
    End Function

    Use like this:

    If SheetExists("MySheet") Then
    ' code to execute if sheet exists
    Else
    ' code to execute if sheet doesn't exist
    End If

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does Sheet Exist (03)

    Expanding on this thought...

    Is it possible to determine if a sheet exists in a closed workbook via VBA? I have a good number of files and do not want to open each one of them via code to determine if a particular sheet exists.

    Thanks,
    John

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Does Sheet Exist (03)

    No.

    Added: well, you could place a formula in the active workbook that refers to a cell on the sheet. If this formula returns #REF, one possible explanation is that the sheet doesn't exist.

Posting Permissions

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