delete all sheets but one

akalei

New Member
Joined
Apr 15, 2005
Messages
16
Hi, I'm trying to delete all the worksheets except for one called "macros" which contains all my macro buttons. I tried using this..

Sub delete_sheets()
bookname = ActiveWorkbook.Name

For i = 1 To ActiveWorkbook.Sheets.Count
Sheets(i).Select
If Sheets(i).Name <> "Macros" Then
ActiveSheet.Delete
End If
Next i

End Sub

I keep getting a subscript out of range error. Does anyone know what that means?

Thanks!
 

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.
application.displayalerts = false
for each ws in thisworkbook.worksheets
if ws.name <> "Macros" then ws.delete
next ws
application.displayalerts = true
 
Upvote 0
Give this a shot instead:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> delete_sheets()<br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br>        Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>            <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets<br>                <SPAN style="color:#00007F">If</SPAN> ws.Name <> "Macros" <SPAN style="color:#00007F">Then</SPAN> ws.Delete<br>            <SPAN style="color:#00007F">Next</SPAN> ws<br>        Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

I would imagine that the subscript out of range error has to do with the method you're trying to use in that you start with a fixed number of sheets, which you then delete, reducing the actual number of sheets in the process. So depending on what sheet you're on VBA is trying to delete a sheet that no longer exists and it bombs because it can't find it.

Hope that helps,
 
Upvote 0
Thanks for the help!! It worked great and I understand what you're saying. I guess I was making my code more complicated than it needed to be.
 
Upvote 0

Forum statistics

Threads
1,214,524
Messages
6,120,049
Members
448,940
Latest member
mdusw

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