🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →
Back to Blog

VBA While Loop in Excel — How to Loop Without Freezing Excel

|

VBA While Loop in Excel — How to Loop Without Freezing Excel

Tested on: Excel 365 v2509 · Excel 2021 · Excel 2019 · last verified 2026-06-05

TL;DR — A While loop repeats code while a condition stays True. Use it when you don't know the row count 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                           ' the step that ends the loop
    Loop
End Sub

The frozen-Excel horror stories all trace back to one missing line. Understand why and you'll never write one.

The one idea: a loop is a promise that it will end — and you own it

A For i = 1 To 100 loop ends on its own. The For statement owns the counter; it cannot run forever even if you do nothing.

A While loop hands that responsibility to you. It will run exactly as long as its condition is True, and it has no idea how to make that condition flip. So every While loop is a promise — "this will eventually become False" — and you are the one keeping it. An infinite loop isn't a freak accident; it's what happens the instant you break that promise.

That reframing tells you when to reach for a While loop at all: use it for an unknown count — "until the data runs out", "until the file opens", "until the user is done". If you can state the number of passes up front, you want For, and you've removed the chance to break the promise.

The rule that keeps the promise: init, test, step

Every correct While loop has the same three parts. Name them and the infinite loop becomes impossible to write by accident:

i = 2                              ' 1. INIT  — set the variable the test reads
Do While Cells(i, 1).Value <> ""   ' 2. TEST  — the promise: this will go False
    ' ... do the work ...
    i = i + 1                      ' 3. STEP  — move the variable TOWARD the exit
Loop

An infinite loop is always a broken part 3: the step is missing, or it moves away from the exit (decrementing when the test wants the value to grow). "Why won't my loop stop?" has exactly one answer — find the variable in the TEST and check that the STEP pushes it toward False.

Choosing the keyword — same promise, clearer intent

Form Checks the promise Use it for
Do While ... Loop before each pass The modern default. Body may run 0 times.
Do Until ... Loop before (stops when True) Reads as "until done" — often clearer.
Do ... Loop While after each pass When the body must run at least once.
While ... Wend before each pass Legacy — works, but can't Exit, so avoid in new code.

Do While condition and Do Until condition are the same loop said two ways — pick whichever matches how you'd describe it out loud. The one with a real recommendation: prefer Do over While...Wend, because only Do lets you bail out early.

' Check at the end — the body always runs once (e.g. prompt, then validate)
Do
    n = InputBox("Enter a positive number")
Loop While n <= 0

Keeping the promise under real conditions

Exit the moment you're doneExit Do beats a flag variable, and is the reason to prefer Do over While...Wend:

Do While Cells(i, 1).Value <> ""
    If Cells(i, 1).Value < 0 Then
        MsgBox "First negative on row " & i
        Exit Do
    End If
    i = i + 1
Loop

When the exit depends on the outside world, add a second promise: a hard cap. Files, networks and other apps fail intermittently, so a retry loop whose only exit is "it worked" can loop forever:

Dim attempts As Long
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            ' the cap is the backup promise

The attempts < 5 guard is what turns "self-healing" into "can't hang." Any loop that waits on external state should have one.

When the promise breaks: the infinite loop

If nothing inside the loop can make the condition False, Excel shows "not responding" and you've broken the promise. Three checks prevent it every time:

  1. Does the STEP change the variable the TEST reads? (i = i + 1, n = n - 1, read the next record…)
  2. Does the STEP move toward the exit? (grow when the test wants < 10, not shrink)
  3. Does an external-state loop have a max-iteration cap?

Stuck in a runaway loop right now? Press Esc or Ctrl + Break to interrupt the macro, then fix the STEP before re-running.

When you shouldn't be writing the loop at all

A While loop is the right tool for "process until the data ends" — but it's also the single most common way a working macro quietly becomes a frozen workbook. ExcelMaster Agent lets you state the goal instead — "clean every row in column A until the data ends and write the result to column B" — and generates bounded code that keeps the promise for you. Try it free →

FAQ

Why does my VBA While loop never stop? The STEP is missing or moving the wrong way. Find the variable in the condition and make sure something inside the loop pushes it toward the exit value. Add a max-attempts cap as a safety net.

What's the difference between While and Do While? Functionally they're nearly identical at the top of a loop. The practical difference: Do While supports Exit Do for early exits and is the recommended modern syntax; While...Wend is legacy and can't 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 at the first blank.

Should I use Do While or Do Until? Whichever reads more clearly out loud. Do While continues while the condition is True; Do Until continues until it's True. They're interchangeable.