Excel like a finance pro.
VBA · Control flow
Do ... Loop
Repeats while or until a condition holds, when you do not know the count in advance.
When to use it
Do ... Loop repeats while or until a condition holds. Use it when the number of iterations is not known in advance.
The code
- Code
Do While Cells(r, 1).Value <> "" r = r + 1 Loop
Worked examples
Do While
Do While Cells(r, 1).Value <> "" r = r + 1 Loop → Walks down until the first blank
Test before each pass.
Do Until
Do answer = InputBox("Enter a number") Loop Until IsNumeric(answer) → Keeps asking until valid
Runs at least once.
Exit Do
Do While True If done Then Exit Do Loop → An explicit exit
Guard against infinite loops.
Worth knowing
- Do While tests first; Do ... Loop While runs at least once.
- Always change something the condition depends on, or it never ends.
- Ctrl + Break interrupts a runaway loop.
Where it goes wrong
- Infinite loops freeze Excel; press Ctrl + Break.
- Empty cells with formulas returning "" are not blank to <> "".
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.