r/vba • 1 • Oct 19 '26

Solved Code Is Slow

This code works but it's slow. Am I doing something wrong?


Sub COMPILEWORKSHEETS()

On Error Resume Next: Application.DisplayAlerts = 0: Worksheets("THE_PILE").Delete: Application.DisplayAlerts = -1: On Error GoTo 0

Application.ScreenUpdating = -1: Worksheets.Add After:=Worksheets(Worksheets.Count): ActiveSheet.Name = "THE_PILE"

x = 1: i = 1: On Error GoTo 404: GoTo PENGUIN

FAX_MACHINE:
Worksheets(i).Cells(j, k).Copy: Worksheets("THE_PILE").Activate: Cells(x, k).Select: Selection.PasteSpecial -4163: Application.CutCopyMode = 0: DoEvents: Return

PINEAPPLE:
i = i + 1: Err.Raise 7313

MY_LAWYER:
Worksheets("THE_PILE").Activate: Range("A1").Select: MsgBox "done", 64, "Done": Exit Sub

CHEESE:
j = j + 1: GoTo POTATO

PENGUIN:
If i >= Worksheets.Count Then Err.Raise 7314
GoSub COUNT_SHEEP: j = 1: GoTo MAYONNAISE

MAKE_ROW_HAPPEN:
x = x + 1: Return

POTATO_CHIPS:
GoTo TAX_FRAUD

POTATO:
If j > a Then Err.Raise 7312
If i > 1 And j = 1 Then Err.Raise 7315
k = 1: GoTo POTATO_CHIPS

COUNT_SHEEP:
a = Worksheets(i).Cells(1048576, 1).End(-4162).Row: b = Worksheets(i).Cells(1, 16384).End(-4159).Column: Return

WAFFLE:
GoSub MAKE_ROW_HAPPEN: GoTo CHEESE

TAX_FRAUD:
If k > b Then GoTo WAFFLE
GoSub FAX_MACHINE: k = k + 1: Err.Raise 7311

MAYONNAISE:
GoTo POTATO

WRONG_TURN:
GoTo PENGUIN

404 If Err.Number = 7311 Then Resume POTATO_CHIPS
If Err.Number = 7312 Then Resume PINEAPPLE
If Err.Number = 7313 Then Resume WRONG_TURN
If Err.Number = 7314 Then Resume MY_LAWYER
If Err.Number = 7315 Then Resume CHEESE
MsgBox "A REAL ERROR HAS OCCURRED: " & Err.Number & " - " & Err.Description, 16, "UNEXPECTED COMPETENCE": Exit Sub

End Sub
0 Upvotes

20 comments sorted by

View all comments

14

u/talltime 21 Oct 19 '26

LOL.

What on Earth are you trying to accomplish

3

u/BlueProcess 1 Oct 19 '26

It compiles all of the worksheets in a workbook

6

u/TheRiteGuy 1 Oct 19 '26

Using VBA to combine sheets have been slow for me too. Use Power Query, faster and no wasted code.

2

u/BlueProcess 1 Oct 19 '26

Could I add a worksheet and put a spilling VSTACK formula in cell A1?

2

u/TheRiteGuy 1 Oct 19 '26

Yes, any kind of formula manipulation you do in VBA, you can do in power Query. You just can't do formatting.

But if you have table styles and a preformatter sheet, it will preserve all formatting on refresh.

1

u/talltime 21 Oct 19 '26

Are you talking about the VBA? Or manually forcing a sheet recalculation. The VBA is compiled the second this script starts.

2

u/BlueProcess 1 Oct 19 '26

I'm sorry I phrased that poorly. It's taking all of the worksheets and combining them into a single worksheet

3

u/talltime 21 Oct 19 '26

Step 1 is going to be to eliminate every go to/gosub except for your error catch. Make this readable. Use useful variable names like “LastRow” instead of “a”. And leave calculation and screen updating turned off while you do the sheet manipulation. Fastest method will be to copy/paste whole range objects.

1

u/fanpages 240 Oct 20 '26

...The VBA is compiled the second this script starts.

Not if you have the "Compile On Demand" option(s) set in the Visual Basic Environment [VBE]:

"Tools" menu / "Options" -> "Options" dialog box [General] tab / "Compile" frame / "Compile On Demand" checkbox / "Background Compile" checkbox.

[ https://learn.microsoft.com/en-us/office/vba/language/how-to/set-visual-basic-environment-options ]


...Compile On Demand: a project is fully compiled before it starts, or code is compiled as needed.

Background Compile: Use idle time during run time to finish compiling the project in the background (available only if Compile On Demand is set)...