Delete a sheet if it has a specific name

thebigtable

New Member
Joined
Jun 11, 2008
Messages
32
I would like to delete the sheets
TEST
BOARD
CHECK
from a workbook. They may contain all 3 or may only have 1 or 2.
I need to confirm the delete automatically.

thanks,
J@thebigtable
:confused:
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

Code:
Sub DeleteSheet()
Dim WS As Worksheet

Application.DisplayAlerts = False

For Each WS In ActiveWorkbook.Worksheets
    If UCase(WS.Name) = "TEST" Then
        WS.Delete
    ElseIf UCase(WS.Name) = "BOARD" Then
        WS.Delete
    ElseIf UCase(WS.Name) = "CHECK" Then
        WS.Delete
    End If
Next WS

Application.DisplayAlerts = True

End Sub

I set the code up in my personal workbook so I have access to it all the time. If you need it only in one workbook (by the sound of it you need it all the time), paste the code into that workbook.

This would work as well:

Code:
Sub DeleteSheet()
Dim WS As Worksheet

Application.DisplayAlerts = False


For Each WS In ActiveWorkbook.Worksheets
    If UCase(WS.Name) = "TEST" Or UCase(WS.Name) = "BOARD" Or UCase(WS.Name) = "CHECK" Then
        WS.Delete
    End If
Next WS

Application.DisplayAlerts = True

End Sub
 
Last edited:
Upvote 0
May something like this?
Code:
Sub DeleteSheet()
Dim sh As Worksheet
On Error GoTo Trap:
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Sheets
 Select Case sh.Name
  Case "TEST", "BOARD", "CHECK": sh.Delete
  Case Else:
End Select
Next sh
Trap:
Application.DisplayAlerts = True
Exit Sub
End Sub

lenze
 
Upvote 0
Why bother testing the sheet name?
Just delete the sheet and ignore the error when it doesn't exist...

Code:
Application.DisplayAlerts = False
On Error Resume Next
Sheets("TEST").Delete
Sheets("BOARD").Delete
Sheets("Check").Delete
On Error Goto 0
Application.DisplayAlerts = True
 
Upvote 0
What if they may not be Upper case?

J


The UCase(WS.Name) = "XXXX" converts all the tab names to upper case, entering "XXXX" in all uppercase will make the structures the same. VBA is particular about string case, I have found forcing everything to uppercase is the only way to ensure you accomplish what you set out to do.
 
Upvote 0
The UCase(WS.Name) = "XXXX" converts all the tab names to upper case, entering "XXXX" in all uppercase will make the structures the same. VBA is particular about string case, I have found forcing everything to uppercase is the only way to ensure you accomplish what you set out to do.
I prefer not using UCASE, and instead, above and outside all macros at the top of the module, maintain the statement
Option Compare Text
and you won't have to worry about remembering UCASE at every codeline where you otherwise would have.
 
Upvote 0

Forum statistics

Threads
1,214,544
Messages
6,120,126
Members
448,947
Latest member
test111

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