Delete worksheets

  • Thread starter Thread starter andym
  • Start date Start date
A

andym

Can you tell me how to write code to delete all worksheets other tha
those with a pre-specified name. eg all sheets other than Sheet1

Thank
 
Hello Andym,

Sub test()
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Sheets
If ws.Name <> "Sheet1" Then ws.Delete
Next ws
Application.DisplayAlerts = True
End Sub

You can also replace Thisworkbook with ActiveWorkbook or
Workbooks("Book1.xls") depending on what workbook does the sheets you want
to delete are located.

Regards,

Jon-jon
 
Sorry, one more thing. How can I adapt the code to delete everythin
other than Sheets1 and 2? I have tried
If ws.Name <> "Sheet1" Or "Sheet2" Then ws.Delete but this doesn'
work.

Thank
 
if ws.name = "Sheet1" _
or ws.name = "Sheet2" then
'don't kill it
else
ws.delete
end if

or

if ws.name <> "Sheet1" and ws.Name <> "Sheet2" then ws.delete
 
Back
Top