Delete Sheets

lundbhaiz

Active Member
Joined
Feb 16, 2010
Messages
386
how do i delete more than 1 worksheet in a single macro ? i have many sheets to be deleted and i need some more code delete multiple sheets by their names.

here is my part of code which i use to delete 1 worksheet.


Code:
On Error Resume Next 
ThisWorkbook.Sheets("MySheet").Delete 
On Error Goto 0
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Just add more lines to your code.
This will delete more sheets on the same conditions.

Code:
Sub deletesheet()

On Error Resume Next
ThisWorkbook.Sheets("Sheet2").Delete 
ThisWorkbook.Sheets("Sheet3").Delete
ThisWorkbook.Sheets("Sheet4").Delete
On Error GoTo 0

End Sub
 
Upvote 0
Hi,

Try:
Sheets(Array("Sheet6", "Sheet7", "Sheet8")).Select
Sheets("Sheet6").Activate
ActiveWindow.SelectedSheets.Delete

Tip: I recorded this macro when deleting multiple sheets :)

Eddie de Jong
 
Upvote 0
nice! thanks you both of you. i knew warhaug type of answer but it was my last priority as i look for some code like defining an array.
 
Upvote 0
Following could be used to shorten macro.

Code:
Sheets(Array("Sheet6", "Sheet7", "Sheet8")).Delete
 
Upvote 0
Which list is shorter?
Sheets you want to delete
Sheets you DO NOT want to delete
?

If the list of sheets to NOT delete is shrter..

Rich (BB code):
Dim ws As WorkSheet
For Each ws In WorkSheets
    Select Case ws.Name
        Case "This", "That", "Whatever" 'List of sheets you DO NOT want to delete
            'Do Nothing
       Case Else
            ws.Delete
    End Select
Next ws

If the list of sheets you WANT to delete is shorter, then

Rich (BB code):
Dim ws As WorkSheet
For Each ws In WorkSheets
    Select Case ws.Name
        Case "This", "That", "Whatever" 'List of sheets to delete
            ws.Delete
        Case Else
            'Do Nothing
    End Select
Next ws
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,247
Messages
6,054,374
Members
444,721
Latest member
BAFRA77

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