Delete worksheets

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
 
J

JON JON

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
 
A

andym

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
 
D

Dave Peterson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top