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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Banker1

Active Member
Joined
Mar 10, 2005
Messages
463
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

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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

Banker1

Active Member
Joined
Mar 10, 2005
Messages
463
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

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,283
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,191,554
Messages
5,987,245
Members
440,086
Latest member
Mahi786

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
Top