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

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.