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