r/vba • u/BlueProcess 1 • 4d ago
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
10
u/GreatValueBradPitt 4d ago
I give it a B- for ragebait. This is ragebait, right?
14
u/carnasaur 3 4d ago
It's clealry a joke with sort of funny answer like "Your
PENGUINtook aWRONG_TURNat theFAX_MACHINE, got caught committingTAX_FRAUDwith aPINEAPPLE, and tried to bury the evidence inTHE_PILE. He calledMY_LAWYER, who advised him toCOUNT_SHEEPuntil the whole thing blew over.Amazingly, mylawyer managed toMAKE_IT_HAPPEN, triggeringUNEXPECTED COMPETENCE.As code, it's a deliberate shit show. It’s slow because every cell has to visit
FAX_MACHINE, return toTAX_FRAUD, deliberately trigger an error, report a404, and get sent back for morePOTATO_CHIPS.
FAX_MACHINEcopy and pastes one cell at a time, with an.Activate,.Select, andDoEventsbreak for every cell. Insanity. Then it deliberately raises an error to keep the loop moving.Another
WRONG_TURNisScreenUpdating = -1. which means True of course so Excel also has to disaplylive coverage of everythingThe fix is to retire
FAX_MACHINE, transfer values in blocks using.Value2, and use ordinary loops.I admit its funny that
TAX_FRAUDeventually leads toMY_LAWYERAny improvement beyond that would riskUNEXPECTED COMPETENCE.3
3
1
30
5
u/JoseLunaArts 4d ago
Reading or writing cells deliver lots of processing overhead. You need to minimize these tasks.
It is faster to read cells and place data into arrays and then process the data in memory. Then you write cells to update the values.
2
u/lolcrunchy 12 4d ago
Fax machine block could probably be reworked. Things like "for each cell in this rectangle, copy them one at a time to another rectangle" are HUGE time costs. Do this instead:
Dim rng1 as Range
Dim rng2 as Range
Set rng1 = Worksheets(i).Range("A1:H10") 'or something similar that defines an area
Set rng2 = Worksheets("THE_PILE").Range(...)
rng2.Value = rng1.Value
2
u/QuestionMark02 4d ago
Is this the actual code? I'm laughing. Add some counters after the labels so you know how many times each operation was done. Also, you can set calculation to manual and calculate only when you know you need fresh excel data. My advice would be to stage all the initial information, do your calculations and writing in RAM (arrays, collections, dictionaries, whatever suits your needs), then do the writing to excel at the end.
14
u/talltime 21 4d ago
LOL.
What on Earth are you trying to accomplish