Test or check if sheet exists

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

    As checking for members of a collection is a general problem, here is an abstracted version of Tim's answer:

    Function Contains(objCollection As Object, strName as String) As Boolean
        Dim o as Object
        On Error Resume Next
        set o = objCollection(strName)
        Contains = (Err.Number = 0)
        Err.Clear
     End Function
    

    This function can be used with any collection like object (Shapes, Range, Names, Workbooks, etc.).

    To check for the existence of a sheet, use If Contains(Sheets, "SheetName") ...

提交回复
热议问题