Check for sheet names, then delete

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
696
How would I go about getting the below code to check first if the sheet exists and skip the delete function if it does not exist?

My users are getting an error because sometimes the sheet does not exist.

Code:
Sub DELETEShts()
    Application.DisplayAlerts = False
    Sheets("AAA").Select
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = False
    Sheets("BBB").Select
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = False
    Sheets("CCC").Select
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
End Sub
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Barrie Davidson

MrExcel MVP
Joined
Feb 10, 2002
Messages
2,330
No need to select the sheets. Try
Code:
Sub DELETEShts()
    Application.DisplayAlerts = False
    On Error Resume Next
    Sheets("AAA").Delete
    Sheets("BBB").Delete
    Sheets("CCC").Delete
    Application.DisplayAlerts = True
End Sub

Regards,
 

Watch MrExcel Video

Forum statistics

Threads
1,118,020
Messages
5,569,664
Members
412,286
Latest member
kychemist00
Top