Test or check if sheet exists

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

    You don't need error handling in order to accomplish this. All you have to do is iterate over all of the Worksheets and check if the specified name exists:

    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = "MySheet" Then
            exists = True
        End If
    Next i
    
    If Not exists Then
        Worksheets.Add.Name = "MySheet"
    End If
    

提交回复
热议问题