Test or check if sheet exists

后端 未结 22 2567
深忆病人
深忆病人 2020-11-21 23:51
Dim wkbkdestination As Workbook
Dim destsheet As Worksheet

For Each ThisWorkSheet In wkbkorigin.Worksheets 
    \'this throws subscript out of range if there is not         


        
相关标签:
22条回答
  • 2020-11-22 00:23

    Put the test in a function and you will be able to reuse it and you have better code readability.

    Do NOT use the "On Error Resume Next" since it may conflict with other part of your code.

    Sub DoesTheSheetExists()
        If SheetExist("SheetName") Then
            Debug.Print "The Sheet Exists"
        Else
            Debug.Print "The Sheet Does NOT Exists"
        End If
    End Sub
    
    Function SheetExist(strSheetName As String) As Boolean
        Dim i As Integer
    
        For i = 1 To Worksheets.Count
            If Worksheets(i).Name = strSheetName Then
                SheetExist = True
                Exit Function
            End If
        Next i
    End Function
    
    0 讨论(0)
  • 2020-11-22 00:23
    Public Function WorkSheetExists(ByVal strName As String) As Boolean
       On Error Resume Next
       WorkSheetExists = Not Worksheets(strName) Is Nothing
    End Function
    
    sub test_sheet()
    
     If Not WorkSheetExists("SheetName") Then
     MsgBox "Not available"
    Else MsgBox "Available"
    End If
    
    End Sub
    
    0 讨论(0)
  • 2020-11-22 00:24

    If you are a fan of WorksheetFunction. or you work from a non-English country with a non-English Excel this is a good solution, that works:

    WorksheetFunction.IsErr(Evaluate("'" & wsName & "'!A1"))
    

    Or in a function like this:

    Function WorksheetExists(sName As String) As Boolean
        WorksheetExists = Not WorksheetFunction.IsErr(Evaluate("'" & sName & "'!A1"))
    End Function
    
    0 讨论(0)
  • 2020-11-22 00:26

    If you are specifically interested in worksheets only, you can use a simple Evaluate call:

    Function WorksheetExists(sName As String) As Boolean
        WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
    End Function
    
    0 讨论(0)
  • 2020-11-22 00:27

    Many years late, but I just needed to do this and didn't like any of the solutions posted... So I made one up, all thanks to the magic of (SpongeBob rainbow hands gesture) "Evaluate()"!

    Evaluate("IsError(" & vSheetName & "!1:1)")
    

    Returns TRUE if Sheet does NOT exist; FALSE if sheet DOES exist. You can substitute whatever range you like for "1:1", but I advise against using a single cell, cuz if it contains an error (eg, #N/A), it will return True.

    0 讨论(0)
  • 2020-11-22 00:29

    My solution looks much like Tims but also works in case of non-worksheet sheets - charts

    Public Function SheetExists(strSheetName As String, Optional wbWorkbook As Workbook) As Boolean
        If wbWorkbook Is Nothing Then Set wbWorkbook = ActiveWorkbook 'or ThisWorkbook - whichever appropriate
        Dim obj As Object
        On Error GoTo HandleError
        Set obj = wbWorkbook.Sheets(strSheetName)
        SheetExists = True
        Exit Function
    HandleError:
        SheetExists = False
    End Function

    .

    0 讨论(0)
提交回复
热议问题