Test or check if sheet exists

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

    I actually had a simple way to check if the sheet exists and then execute some instruction:

    In my case I wanted to delete the sheet and then recreated the same sheet with the same name but the code was interrupted if the program was not able to delete the sheet as it was already deleted

    Sub Foo ()
    
        Application.DisplayAlerts = False
    
        On Error GoTo instructions
        Sheets("NAME OF THE SHEET").Delete
    
        instructions:
    
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = "NAME OF THE SHEET"
    
    End Sub
    

提交回复
热议问题