Error Handling When Deleting Sheets

mayoung

Active Member
Joined
Mar 26, 2014
Messages
259
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If there is no matching sheet to be deleted, then your code won't generate an error.

Instead, you could increment a count for each sheet you delete, and then test if count = 0.
 
Upvote 0
If there is no matching sheet to be deleted, then your code won't generate an error.

Instead, you could increment a count for each sheet you delete, and then test if count = 0.
Not Sure how to apply this?
 
Upvote 0
At its simplest:

VBA Code:
Dim wsheet As Worksheet
Dim count As Long

For Each wsheet In ActiveWorkbook.Worksheets
    If UCase(wsheet.Name) Like "*SHEET*" Then
        Application.DisplayAlerts = False
        wsheet.Delete
        count = count + 1
        Application.DisplayAlerts = True
    End If
Next

MsgBox count & " sheet" & IIf(count = 1, "", "s") & " deleted", vbInformation

Or if you want to take different actions depending on the value of count:

Code:
'....

If count = 0 Then
    'No sheets deleted
    'Do something?
Else
    'Do something different?
End If
 
Upvote 0
Solution
At its simplest:

VBA Code:
Dim wsheet As Worksheet
Dim count As Long

For Each wsheet In ActiveWorkbook.Worksheets
    If UCase(wsheet.Name) Like "*SHEET*" Then
        Application.DisplayAlerts = False
        wsheet.Delete
        count = count + 1
        Application.DisplayAlerts = True
    End If
Next

MsgBox count & " sheet" & IIf(count = 1, "", "s") & " deleted", vbInformation

Or if you want to take different actions depending on the value of count:

Code:
'....

If count = 0 Then
    'No sheets deleted
    'Do something?
Else
    'Do something different?
End If
Thank You Very Much..
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top