Macro to delete an array of worksheets

AlisaA

Board Regular
Joined
Mar 17, 2010
Messages
193
I am working with a particularly ugly macro that deletes an array of sheets in my workbook (I wrote it, so I don't feel bad insulting it, haha).

I started off by recording it, which gave me code that does the job, but you can imagine how ugly it turned out:

'Delete weekly data
Sheets("p12").Select
Sheets(Array("p12", "p12w4", "p12w3", "p12w2", "p12w1", "p11", "p11w4", "p11w3", _
"p11w2", "p11w1", "p10", "p10w5", "p10w4", "p10w3", "p10w2", "p10w1", "p09", "p09w4", _
"p09w3", "p09w2", "p09w1", "p08", "p08w4", "p08w3", "p08w2")).Select
Sheets("p12").Activate
Sheets(Array("p08w1", "p07", "p07w5", "p07w4", "p07w3", "p07w2", "p07w1", "p06", _
"p06w4", "p06w3", "p06w2", "p06w1", "p05", "p05w4", "p05w3", "p05w2", "p05w1", "p04", _
"p04w5", "p04w4", "p04w3", "p04w2", "p04w1", "p03", "p03w4")).Select Replace:=False
Sheets(Array("p03w3", "p03w2", "p03w1", "p02", "p02w4", "p02w3", "p02w2", "p02w1", _
"p01", "p01w5", "p01w4", "p01w3", "p01w2", "p01w1")).Select Replace:=False
Sheets(Array("p12", "p12w4", "p12w3", "p12w2", "p12w1", "p11", "p11w4", "p11w3", _
"p11w2", "p11w1", "p10", "p10w5", "p10w4", "p10w3", "p10w2", "p10w1", "p09", "p09w4", _
"p09w3", "p09w2", "p09w1", "p08", "p08w4", "p08w3", "p01w1")).Select
Sheets("p01w1").Activate
Sheets(Array("p08w2", "p08w1", "p07", "p07w5", "p07w4", "p07w3", "p07w2", "p07w1", _
"p06", "p06w4", "p06w3", "p06w2", "p06w1", "p05", "p05w4", "p05w3", "p05w2", "p05w1", _
"p04", "p04w5", "p04w4", "p04w3", "p04w2", "p04w1", "p03")).Select Replace:=False
Sheets(Array("p03w4", "p03w3", "p03w2", "p03w1", "p02", "p02w4", "p02w3", "p02w2", _
"p02w1", "p01", "p01w5", "p01w4", "p01w3", "p01w2")).Select Replace:=False
ActiveWindow.SelectedSheets.Delete

I know this is horribly inefficient code, but I am struggling to rewrite it. One of the issues is, the sheets were not necessarily created in consecutive order, so their sheet numbers are not in consecutive order.

They are in order in the workbook itself, so I did run a Debug.Print Sheets(i).Name which gave me a list of my sheets in proper order in the VBA Immediate pane, which I then narrowed down to the ones I want in this array.

But now I'm not sure where to go from here.

I appreciate the help, sage advice! Thank you!!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Does this work?

Code:
Sheets(Array("p12", "p12w4", "p12w3", "p12w2", "p12w1", "p11", "p11w4", "p11w3", _
"p11w2", "p11w1", "p10", "p10w5", "p10w4", "p10w3", "p10w2", "p10w1", "p09", "p09w4", _
"p09w3", "p09w2", "p09w1", "p08", "p08w4", "p08w3", "p08w2")).Delete
 
Upvote 0
Yes, that worked just fine! Can I put all of the sheets I want to delete in the same array, or am I limited to a certain number?

Thanks!!
 
Upvote 0
IF there is a limit (not sure if there is) it is probably 255 or similar.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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