Macro to Delete Unwanted Sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I am looking for Code to Delete all Sheets except sheets starting with BCM, Con, Niss & Consolidated BCM


Your assistance is most appreciated
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:

Please check the sheet names as they will stay. I would also recommend saving a second copy of the file before running the code just in case of the file name typo
was not sure if "Niss & Consolidated BCM" was one sheet or its suppose to be "Niss" and "Consolidate BCM" as another sheet. if that's 2 sheets please modify code to add the other exceipion
VBA Code:
Sub DeleteSheets1()
    Dim xWs As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> "BCM" And xWs.Name <> "Con" And xWs.Name <> "Niss & Consolidated BCM" Then
            xWs.Delete
        End If
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Many Thanks for your help

The Sheets to remain are those starting with BCM eg BCM1, BCM2 etc , Con, Niss and Consolidated BCM

See link below showing the sheet. I have highlighted the sheets to remain


Kindly test & amend code
 
Upvote 0
Try first on a copy of your workbook.
Code:
Sub Maybe()
Dim ws As Worksheet
Application.DisplayAlerts = False
    For Each ws In ActiveWorkbook.Worksheets
        If Not (ws.Name Like "BCM*" Or ws.Name Like "Con*" Or ws.Name Like "Consolidated BCM*" Or ws.Name Like "Niss*") Then ws.Delete
    Next ws
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
I could not open your attachment before but it looks like you might have spaces in your sheet names.
I also thought you meant sheets starting with BCM, starting with Con, starting with Nis and starting with Consolidated BCM.
That looks to be not the case so you can delete the wildcard character (*) from these.
So that line should be
Code:
If Not (ws.Name Like "BCM*" Or ws.Name Like "Con" Or ws.Name Like "Consolidated BCM" Or ws.Name Like "Niss") Then ws.Delete
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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