Delete All Sheets Except One

kubrendan

New Member
Joined
Jan 15, 2007
Messages
39
How can I delete all worksheets in a workbook except for one with a given name? The titles of the other worksheets will vary, so I'm not sure how to do it (I only know how to delete based on a name; in this case, I want to always delete all worksheets except for "Main").

Thanks in advance!

--Brendan--
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Sub deleteSheets()
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
    If Not ws.Name = "Main" Then
        ws.Delete
    End If
Next ws

Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
Tr
Code:
Sub test()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Main" Then ws.Delete
Next ws
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,495
Messages
6,160,142
Members
451,624
Latest member
TheWes

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