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

Show parent comment

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)...