Delete Surplus Sheets

Damo71

Board Regular
Joined
Aug 17, 2010
Messages
88
Hi

Hopefuly this is simple for VBA guys...

What I need to do is simply automatically delete all sheets except Sheet1, 2 & 3

Thanks

Damian
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try

Code:
Sub test2()
Dim i As Long
Application.DisplayAlerts = False
For i = Worksheets.Count To 4 Step -1
    Worksheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Try:
Code:
Sub KillAllTheHumans ()
Dim i as Long
 
Application.DisplayAlerts = False
 
For i = 1 to Worksheets.Count
  Select Case Sheets(i).Name
     Case "Sheets1", "Sheets2", "Sheets3"
     Case Else
          Sheets(i).Delete
  End Select
Next i
 
Application.DisplayAlerts = True
 
End Sub
This may not work but my IT at work is totally messed up today so can't text anything and doing all this from guesswork. If doesn't work, hopefully someone else can help...
 
Upvote 0
Something like:-
Code:
option explicit
option compare text
sub deletesheets()
  dim ws as worksheet
  for each ws in thisworkbook.worksheets
    if ws.name="sheet1" or ws.name="sheet2" or ws.name="sheet3" then
      ' do nothing
    else
      application.displayalerts=false
      ws.delete
      application.displayalerts=true
    endif
  next ws
end sub
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,855
Members
452,948
Latest member
UsmanAli786

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