Excel like a finance pro.
VBA · Control flow
Exit For, Exit Do, Exit Sub
Leaves a loop or procedure early.
When to use it
Exit For, Exit Do, Exit Sub, and Exit Function leave the current loop or procedure immediately.
The code
- Code
If found Then Exit For
Worked examples
Stop when found
For Each c In rng If c.Value = target Then found = c.Address Exit For End If Next c → Stops at the first match
No wasted passes.
Guard clause
If lastRow < 2 Then Exit Sub → A guard clause at the top
Nothing to do, leave.
Before an error handler
On Error GoTo Handler ... Exit Sub Handler: MsgBox Err.Description → Exit Sub stops normal flow from falling into the handler
The standard error pattern.
Worth knowing
- Guard clauses keep the main code un-nested.
- Exit Sub before a handler label is essential.
- End (alone) stops all code and resets variables; avoid it.
Where it goes wrong
- Exit inside nested loops only leaves the inner one.
- Exit Sub skips cleanup like restoring ScreenUpdating; put cleanup in the handler path too.
Related
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.