Tested on: Excel 365 v2509 · Excel 2021 · Excel 2019 · last verified 2026-06-05
TL;DR — A While loop repeats code as long as a condition stays True. Use it when you don't know the number of rows in advance. Copy this and adapt:
Sub LoopUntilBlank()
Dim i As Long
i = 2 ' start on row 2
Do While Cells(i, 1).Value <> "" ' run while column A has data
Cells(i, 2).Value = Cells(i, 1).Value * 1.1
i = i + 1 ' MUST advance, or the loop never ends
Loop
End Sub
That i = i + 1 line is the whole game. Forget it and Excel freezes. The rest of this guide shows every While variant and how to never hang Excel again.
Why use a While loop instead of For?
A For i = 1 To 100 loop is for a known count. A While loop is for an unknown count — "keep going until the data runs out," "retry until the file opens," "process until the user cancels." If you can't say how many iterations up front, you want While.
The three loop keywords (and which to pick)
| Form | Checks condition | Notes |
|---|---|---|
Do While ... Loop |
Before each pass | Body may run 0 times. The modern default. |
Do ... Loop While |
After each pass | Body always runs at least once. |
Do Until ... Loop |
Before (stops when True) | Reads naturally: "until done." |
While ... Wend |
Before each pass | Legacy. Works, but Microsoft recommends Do instead. |
Recommendation: use
Do While/Do Until. The oldWhile...Wendsurvives only for backward compatibility — it can't useExitand is considered deprecated style.
Example 1 — Do While vs Do Until (same result, clearer intent)
' Do While — "while there is still data"
Do While Cells(i, 1).Value <> ""
' ... process row i
i = i + 1
Loop
' Do Until — "until we hit a blank" (often easier to read)
Do Until Cells(i, 1).Value = ""
' ... process row i
i = i + 1
Loop
Both stop at the first empty cell in column A. Pick whichever reads more naturally to you — they compile to the same logic.
Example 2 — Check the condition at the end
Sometimes you must run the body at least once — for example, prompt the user, then validate:
Sub AskForPositive()
Dim n As Double
Do
n = InputBox("Enter a positive number")
Loop While n <= 0 ' keeps re-asking until they comply
End Sub
Because the test is on the Loop While line, the InputBox always shows once before the condition is ever checked.
Example 3 — While...Wend (legacy, for when you inherit old code)
Sub LegacySum()
Dim total As Double, i As Long
i = 2
While Cells(i, 1).Value <> ""
total = total + Cells(i, 1).Value
i = i + 1
Wend
MsgBox "Total: " & total
End Sub
This works on every Excel version, but if you're writing new code, convert it to Do While ... Loop so you can use Exit Do for early exits.
Example 4 — Exit the loop early
Sub FindFirstNegative()
Dim i As Long
i = 2
Do While Cells(i, 1).Value <> ""
If Cells(i, 1).Value < 0 Then
MsgBox "First negative on row " & i
Exit Do ' bail out immediately
End If
i = i + 1
Loop
End Sub
Exit Do jumps out the moment you've found what you need — no flag variable required. (While...Wend can't do this, which is the main reason to prefer Do.)
Example 5 — Retry with a safety counter
Real automation talks to files, networks, and other apps that fail intermittently. Always cap your retries so a permanent failure can't loop forever:
Sub OpenWithRetry()
Dim attempts As Long
attempts = 0
Do
attempts = attempts + 1
On Error Resume Next
Workbooks.Open "C:\reports\daily.xlsx"
On Error GoTo 0
If Not Workbooks Is Nothing Then Exit Do
Loop While attempts < 5 ' never more than 5 tries
End Sub
The attempts < 5 guard is the difference between "self-healing" and "hung forever."
The #1 trap: the infinite loop
A While loop runs until its condition becomes False. If nothing inside the loop can ever make that happen, Excel hangs and you'll see "Excel is not responding."
Three rules that prevent it every time:
- Always change the variable in the condition inside the loop (
i = i + 1,n = n - 1, read the next record…). - Make sure the change moves toward the exit — incrementing when the condition tests
< 10, not decrementing. - Add a max-iteration counter for any loop whose end depends on external state (files, user input, API calls).
Stuck in a runaway loop right now? Press Esc or Ctrl + Break to interrupt the macro, then fix the condition before re-running.
Stop babysitting loop counters — describe the goal instead
While loops are where "it worked yesterday" macros quietly turn into frozen spreadsheets — one missing increment and the whole workbook locks. ExcelMaster Agent lets you state the outcome in plain English — "clean every row in column A until the data ends and write the result to column B" — and it generates safe, bounded code that can't hang Excel. Try it free →
Related guides
- VBA For Loop in Excel — 8 Real-World Examples
- VBA If Then Else — 6 Real-World Examples
- VBA Select Case — A Complete Guide with Examples
FAQ
What's the difference between While and Do While in VBA?
Functionally they're almost identical when the test is at the top. The practical difference: Do While supports Exit Do for early exits and is the recommended modern syntax; While...Wend is legacy and cannot exit early.
How do I loop until a cell is empty?
Do While Cells(i, 1).Value <> "" (or Do Until Cells(i, 1).Value = ""), incrementing i each pass. Stop when you reach the first blank.
Why does my VBA While loop never stop? The variable in the condition isn't changing inside the loop, or it's moving away from the exit value. Add or fix the increment line, and add a max-attempts counter as a safety net.
Should I use Do While or Do Until?
Whichever reads more clearly. Do While condition continues while True; Do Until condition continues until True. They're interchangeable — pick the one that matches how you'd say it out loud.
