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!!
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
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
 

AlisaA

Board Regular
Joined
Mar 17, 2010
Messages
193
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!!
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
IF there is a limit (not sure if there is) it is probably 255 or similar.
 

AlisaA

Board Regular
Joined
Mar 17, 2010
Messages
193
Still, your code is a huge improvement. Thanks so much for your help! I appreciate it!!
 

Forum statistics

Threads
1,082,017
Messages
5,362,695
Members
400,686
Latest member
Aakash

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top