Code for deleting worksheets

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,341
I have a workbook which creates 52 worksheets each year. At the end of the year, I wish to delete them. They are all contiguous and typically occupy sheet index numbers 11 to 62 or 12 to 63. Can anyone suggest an efficient code to delete them all. I know it's only once a year, but I don't want to have to make a progress bar. Is it simply best to work through them and delete them one at a time or could I group them by adding them to a collection or an expanding array and then delete the lot in one go. Is that even possible? Also, I don't want to move the remaining worksheets to a new workbook as I don't know what links someone might add during the year. Any suggestions will be gratefully received.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If you want to delete them by the index numbers try something like:
VBA Code:
Application.DisplayAlerts = False
    
    Sheets(Array(2, 3, 4, 5)).Delete
    
Application.DisplayAlerts = True

If it's always the last x sheets you want to delete try something like:
VBA Code:
Dim i As Integer
Dim First As Integer
Dim Last As Integer

Last = Worksheets.Count
First = Last - 5    'Deletes the last 5 sheets

Application.DisplayAlerts = False
    
For i = First To Last
    Sheets(First).Delete
Next i
    
Application.DisplayAlerts = True
 
Upvote 0
Hi Misca,
Many thanks for your very detailed reply. I'll experiment with them both. I can make it a rule that no sheets may be added on the right hand side and it should not be a problem after a few weeks as that end of the workbook will not be so tempting for people to use anyway. I'm hoping that the Array, albeit with 52 members, will be the faster method as loops always seem to be so slow.
 
Upvote 0
Hi Misca,
That was interesting, I have tried them both, removing 52 worksheets, and to the nearest second, they both came in as instantaneous. Not what I was expecting at all. Thanks again for your help.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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