Looping through worksheets and delete

OscarJr15

New Member
Joined
Mar 7, 2007
Messages
49
Hello,

I have workbook that I send out every week and I want to be able to delete worksheets whose names begin with the letter "x". I don't want to do this manually every week and would love to automate the process some how.

Any help would be greatly appreciated.

Thanks
 

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
Try the following:
Code:
Sub DeleteSheets()
Dim lngKount As Long
'
Application.DisplayAlerts = False
'
For lngKount = Sheets.Count To 1 Step -1
    If Left(Sheets(lngKount).Name, 1) = "x" Then
        Sheets(lngKount).Delete
    End If
Next lngKount
'
Application.DisplayAlerts = True
'
End Sub
When deleting worksheets, it is safer to start with the last and work backwards.
If you should have all worksheet names starting with 'x' you will get an error because you must have at least one in the workbook.
Important: test this on a copy of your workbook first.
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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