VBA - adjust to not delete all sheets

JohnDouglas

Board Regular
Joined
Jan 5, 2005
Messages
239
Hi guys

i have hte following line of code that deletes all sheets that aren't named "Main".
Code:
   For I = ActiveWorkbook.Worksheets.Count To 1 Step -1
      On Error Resume Next
      If Worksheets(I).Name <> "Main" Then _
         Worksheets(I).Delete
On Error Resume Next
   Next I

But, i'd like a couple of other sheets to not be deleted too. such as "Main 2" etc. Is it possible to adjust this code so that it won't delete any sheet named in an array or any sheet that contains "Main" in its name?

Thanks for your help

John
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi John

If you mean that those names start by Main, use

Code:
If Left(Worksheets(I).Name,4) <> "Main" Then _

HTH
PGC
 
Upvote 0
John

You could test for Main being in the name like this.
Code:
For Each ws In Worksheets
     If InStr(ws.Name, "Main")= 0 Then ws.Delete
Next ws
If you want to not delete other sheets you might want to look at a Select Case structure.
Code:
For Each ws In Worksheets
     Select Case ws.Name
         Case "Main", "Main 2", "Some other sheet"
               ' do nothing
         Case
               ' ws.Delete
     End Select
Next ws
 
Upvote 0
norie - how does that code work?

i've got it to work, just odn't understand how it's doing it!

Code:
For Each ws In Worksheets
     Select Case ws.Name
         Case "Main", "Main 2", "Some other sheet"
               ' do nothing
         Case ws.Delete
     End Select
Next ws
 
Upvote 0
John

There's actually some typos in it, I just typed it here not in the VBE.:oops:

Bad habit. o_O

Should be this really.
Code:
For Each ws In Worksheets
     Select Case ws.Name
         Case "Main", "Main 2", "Some other sheet"
               ' do nothing
         Case Else
                ws.Delete
     End Select
Next ws
What it does is loop through the worksheets, checks the names of the sheets against the first Case and if the name matches does nothing, otherwise it deletes the sheet.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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