I have two sets of code one for deleting one sheet and another for deleting multiple sheets the code works but the error handler does not work when there are no sheet or sheets, which I need. Any ideas how to modify this so the error handler works?
VBA Code:
'Private Sub DeleteActiveSheetSummary(ctrl As IRibbonControl)
Sub DeleteActiveSheetSummary()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
If ActiveWorkbook.Name = "ServiceFile.xml" Then
On Error GoTo EH:
With ActiveSheet
Application.DisplayAlerts = False
If LCase(.Name) Like "*sheet*" Then .Delete
Application.DisplayAlerts = True
End With
ActiveWindow.ScrollWorkbookTabs Sheets:=-4
Sheets("WIP-SUMMARY").Select
GoTo EJ:
EH:
MsgBox "No Sheet to Delete", vbCritical
EJ:
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
Else
MsgBox "Service File Is Not Active Or Has Not Been Uploaded.", vbCritical
End If
End Sub
Code:
'Private Sub DeleteSheetsSummary(ctrl As IRibbonControl)
Sub DeleteSheetsSummary()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
If ActiveWorkbook.Name = "ServiceFile.xml" Then
On Error GoTo EH:
Dim wsheet As Worksheet
For Each wsheet In ActiveWorkbook.Worksheets
If UCase(wsheet.Name) Like "*SHEET*" Then
Application.DisplayAlerts = False
wsheet.Delete
Application.DisplayAlerts = True
End If
Next
ActiveWindow.ScrollWorkbookTabs Sheets:=-4
Sheets("WIP-SUMMARY").Select
GoTo EJ:
EH:
MsgBox "No Sheets To Delete", vbCritical
EJ:
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
Else
MsgBox "Service File Is Not Active Or Has Not Been Uploaded.", vbCritical
End If
End Sub