ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete worksheets (https://www.excelbanter.com/excel-programming/283418-delete-worksheets.html)

andym

Delete worksheets
 

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

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com


JON JON

Delete worksheets
 
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

"andym" wrote in message
...

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

Thanks


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/




andym

Delete worksheets
 

Thanks Jon-jon, that's perfec

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com


JON JON

Delete worksheets
 
Glad to help

"andym" wrote in message
...

Thanks Jon-jon, that's perfect


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/




andym

Delete worksheets
 

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

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com


Dave Peterson[_3_]

Delete worksheets
 
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





andym wrote:

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

Thanks

------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/


--

Dave Peterson


andym

Delete worksheets
 

Thank you

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com



All times are GMT +1. The time now is 10:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com