Results 1 to 4 of 4

VBA: If worksheet does not exist then...

This is a discussion on VBA: If worksheet does not exist then... within the Excel Questions forums, part of the Question Forums category; In VBA how do I do the following two things. I need syntax for the if sheets(n) statements. Thanks. sub ...

  1. #1
    Board Regular
    Join Date
    Feb 2003
    Posts
    323

    Default VBA: If worksheet does not exist then...

    In VBA how do I do the following two things. I need syntax for the if sheets(n) statements. Thanks.

    sub help()
    n = activecell

    if sheets(n) does not exist in this workbook then...
    rest of code
    end if

    ALSO

    if worksheets(n) exists in this workbook then...
    rest of code
    end if

    end sub

  2. #2
    MrExcel MVP Tom Urtis's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, California USA
    Posts
    11,011

    Default Re: VBA: If worksheet does not exist then...

    I have an FAQs page on my web site, with this question being one of them, see if the first macro on this page helps:

    http://www.atlaspm.com/faqwsnameexist.html#76

  3. #3
    Board Regular
    Join Date
    Feb 2003
    Posts
    323

    Default Re: VBA: If worksheet does not exist then...

    Thank you Tom, I believe that this is exactly what I need.

  4. #4
    Board Regular
    Join Date
    May 2009
    Location
    Texas, USA
    Posts
    1,290

    Default Re: VBA: If worksheet does not exist then...

    Code:
     
    Sub TestForWorksheet()
        Dim ws As Worksheet
        On Error Resume Next
        Set ws = Sheets("MySheet")
        If Err.Number <> 0 Then
            MsgBox "Worksheet does not exist"
        Else
            MsgBox "Worksheet exists"
        End If
    End Sub
    Bill
    Use Option Explicit to avoid chasing code.

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