+ Reply to Thread
Results 1 to 4 of 4

Determining if a worksheet exists

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Question Determining if a worksheet exists

    Hi when I try to do the following I get an error 'Subscript out of range'

        If Not .Worksheets(ws_name_string) Is Nothing Then
    To do this do I HAVE to then have a worksheet variable to test against, such as:

    Dim ws as Worksheet
    
    With ThisWorkbook
        on error resume next
        Set ws = .worksheets(ws_name_string) 
        on error goto 0
        If Not ws Is Nothing Then
        ...
        ...
        End If
    End With
    I was hoping I might be able to achive the same test within one line?

    Thanks
    Last edited by Phil_V; 06-11-2009 at 06:06 AM.
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Determining if a worksheet exists

    You could possibly think of going down the Evaluate route

    If Not IsError(Evaluate("COUNTBLANK('" & ws_name_string & "'!A1)")) Then
    
    End If
    The use of COUNTBLANK should cater for A1 on sheet of interest containing an error value if exists.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Determining if a worksheet exists

    Here's a UDF that tests if a worksheet exists

    Function WksExists(wksName As String) As Boolean
        On Error Resume Next
        WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
    End Function
    Use it like this
    If Not WksExists("test") Then
    Msgbox "Not there"
    Else: 'o something else
    End If
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Determining if a worksheet exists

    Cheers all

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1