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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

zzjasonzz

Well-known Member
Joined
Apr 23, 2006
Messages
649
application.displayalerts = false
for each ws in thisworkbook.worksheets
if ws.name <> "Macros" then ws.delete
next ws
application.displayalerts = true
 
Upvote 0

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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

akalei

New Member
Joined
Apr 15, 2005
Messages
16
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,191,165
Messages
5,985,040
Members
439,935
Latest member
Monty238

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
Top