+ Reply to Thread
Results 1 to 6 of 6

If Sheet Exists Error

Hybrid View

ptmuldoon If Sheet Exists Error 07-29-2015, 11:05 AM
Faridwahidi Re: If Sheet Exists Error 07-29-2015, 11:19 AM
Ron Coderre Re: If Sheet Exists Error 07-29-2015, 11:19 AM
LJMetzger Re: If Sheet Exists Error 07-29-2015, 11:21 AM
ptmuldoon Re: If Sheet Exists Error 07-29-2015, 11:22 AM
Faridwahidi Re: If Sheet Exists Error 07-29-2015, 11:24 AM
  1. #1
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    863

    If Sheet Exists Error

    I'm trying to test if a Sheet Exists, and if it does exits to do some additional code/work on it. Any reason why the below fails with a 'Subscript out of Range' Error when the sheet in question is missing?

    If I change the Sheet name to something that does exists, the code and msgbox returns correctly. But when the sheet being looked for is actually missing it gives the error?

    Sub Test()
        If Not Sheets("Evaluation") Is Nothing Then
            MsgBox "Sheet Exists"
        Else
            MsgBox "Missing"
        End If
    End Sub
    Edit. I think I understand now. This is because it is looking at the ActiveSheet only? And you need to actually loop through the workbook to check if the worksheet exists?
    Last edited by ptmuldoon; 07-29-2015 at 11:13 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: If Sheet Exists Error

    Hi ptmuldoon,

    you may try this,

    Sub checkSheet()
        If Not SheetExists("Evaluation") Then
            MsgBox "Missing", vbInformation, "Sheet Not Found"
        End If
    End Sub
    
    Function SheetExists(WorkSheetName) As Boolean
        SheetExists = False
        For Each ws In Sheets
            If UCase(ws.Name) = UCase(WorkSheetName) Then SheetExists = True
        Next ws
    End Function
    If this helped you, please add reputation to me

  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: If Sheet Exists Error

    I use this:
    Function SheetNameExists(strShtName As String, Optional strWkbkName) As Boolean
    If IsMissing(strWkbkName) Then
       strWkbkName = ThisWorkbook.Name
    End If
        
    On Error Resume Next
    SheetNameExists = Workbooks(strWkbkName).Sheets(strShtName).Parent.Name <> ""
            
    End Function
    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: If Sheet Exists Error

    Hi,

    The subscript Out of Range runtime error is generated when the Sheet does not exist.

    I use the following function:
    Public Function LjmSheetExists(SheetName As String) As Boolean
    'Return value TRUE if sheet exists
    
      On Error Resume Next
    
      If Sheets(SheetName) Is Nothing Then
        LjmSheetExists = False
      Else
        LjmSheetExists = True
      End If
      On Error GoTo 0
      
    End Function
    Lewis

  5. #5
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    863

    Re: If Sheet Exists Error

    Both are good solutions. Code is running now through about 100 files to see if the sheets exists and pull out some data.

  6. #6
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: If Sheet Exists Error

    Hi pthmuldoon,

    Thanks for rep'

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 07-27-2015, 04:27 PM
  2. Check if Sheet Name exists Then Error or continue Create
    By alimsab in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2015, 02:55 PM
  3. Replies: 1
    Last Post: 11-14-2013, 09:24 PM
  4. if sheet name exists then exit sub - error
    By CobertRannon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-29-2013, 03:34 PM
  5. Replies: 1
    Last Post: 05-29-2013, 01:20 PM
  6. [SOLVED] Check if sheet exists and delete it using IF ERROR function in VBA
    By Jardim in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-25-2012, 12:06 PM
  7. Macro to copy sheet from closed work book and overwrite if sheet already exists
    By munkee in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-23-2009, 06:36 PM

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