Test or check if sheet exists

后端 未结 22 2591
深忆病人
深忆病人 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: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

    .

提交回复
热议问题