Case Statement with Wild Cards

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to delete sheets that have specific names or similar (e.g. delete "Sheet1" and "Sheet1 (2)") I am using the following code:

Code:
For Each ws In Worksheets
    Select Case ws.Name
        Case "Remaining*", _
                "No_Break*", _
                "Listed_Instruments*", _
                "Net_Zero_Difference*", _
                "One_Dodge_Many_FO*", _
                "Many_Dodge_One_FO*", _
                "Materiality*"
                ws.Delete
    End Select
Next ws
But whilst it is working without any breaks, it is not deleting the sheets are required. The above are the only sheets that need to be deleted.

Could someone explain why it's not working please?

Thanks in advance,
Jack
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
That is looking at the literal values. To use wildcards you need something like

Code:
For Each ws In Worksheets
    If ws.Name Like "Remaining*" Or ws.Name Like "No_Break*" _
                Or ws.Name Like "Listed_Instruments*" _
                Or ws.Name Like "Net_Zero_Difference*" _
                Or ws.Name Like "One_Dodge_Many_FO*" _
                Or ws.Name Like "Many_Dodge_One_FO*" _
                Or ws.Name Like "Materiality*" Then ws.Delete
    End If
Next ws
 
Upvote 0
But is there not a way to use CASE instead of lots of OR tests?
 
Upvote 0
No worries, see if anyone else posts something. It seemed like something suitable for CASE!
Thanks Peter,
Jack
 
Upvote 0
You can do this:

Code:
Sub Test()
    Dim ws As Worksheet
    For Each ws In Worksheets
        Select Case True
            Case ws.Name Like "Remaining*", _
                ws.Name Like "No_Break*", _
                ws.Name Like "Listed_Instruments*", _
                ws.Name Like "Net_Zero_Difference*", _
                ws.Name Like "One_Dodge_Many_FO*", _
                ws.Name Like "Many_Dodge_One_FO*", _
                ws.Name Like "Materiality*"
                ws.Delete
        End Select
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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