VBA Do Until Loop: Syntax, Examples, and Common Errors

VBA & macros · Updated November 3, 2026

VBA Do Until Loop: Syntax, Examples, and Common Errors

A VBA Do Until loop repeats a block of code until a condition becomes true. Use Do Until condition ... Loop to test the condition before each pass, or Do ... Loop Until condition to test it after each pass, which guarantees the code runs at least once.

What a VBA Do Until loop does

A loop is a way to run the same instructions repeatedly without copying them into a macro many times. The Until keyword makes the loop stop when its condition is true. While the condition is false, execution continues through the loop body and then checks the condition again.

For example, a macro might move through rows until it reaches a blank cell, increase a counter until it reaches a target, or keep checking a status value until a process finishes. The important design question is always: what changes during each pass so that the stop condition can eventually become true?

Do Until is not a separate kind of VBA statement from the Do loop family; it is one way to express the loop’s stopping rule. VBA also provides Do While, which repeats while a condition is true, and For loops, which are often convenient when the number of repetitions is known.

Basic syntax and the two forms

The pre-test form checks the condition at the top of the loop:

Do Until condition
    ' Statements to repeat
Loop

If the condition is already true when VBA reaches the loop, the statements are skipped. Otherwise, VBA runs them, returns to the top, and checks again.

The post-test form checks at the bottom:

Do
    ' Statements to repeat
Loop Until condition

Here, VBA runs the statements before the first test, so this form always makes at least one pass. Choose it when the first operation must happen before there is anything meaningful to test, such as reading a value and then deciding whether to continue.

In both forms, the condition is a Boolean expression: it evaluates to True or False. Comparisons such as counter >= 5, cellValue = "Done", and IsEmpty(value) can be used when their values make sense for the task.

How the condition changes and stops the loop

A Do Until loop does not automatically move to another row, increase a counter, or wait for an Excel calculation. The statements inside it must update something that affects the condition. If nothing changes and the condition starts false, the macro can run indefinitely.

This counter example stops after the counter reaches 5. The loop runs five times because it begins at zero and adds one on every pass:

Sub CountToFive()
    Dim counter As Long

    counter = 0
    Do Until counter >= 5
        counter = counter + 1 ' Move toward the stopping condition
    Loop
End Sub

If the increment line were removed, counter >= 5 would remain false and the loop would never finish. If the counter started at 5 or more, the pre-test form would finish without running its body.

Conditions can also depend on worksheet contents. Be precise about what counts as an empty cell: IsEmpty is true for a cell with no value, but not for a formula cell whose displayed result is an empty string. Errors in a cell may also require separate handling rather than a simple text comparison.

Example: process rows until a blank cell

This macro marks column B for each consecutive nonempty entry in column A, starting at row 2. It stops at the first genuinely empty cell in column A:

Sub MarkRowsUntilBlank()
    Dim ws As Worksheet
    Dim rowNum As Long

    Set ws = ThisWorkbook.Worksheets("Data")
    rowNum = 2

    Do Until IsEmpty(ws.Cells(rowNum, 1).Value)
        ws.Cells(rowNum, 2).Value = "Checked" ' Write beside the entry
        rowNum = rowNum + 1 ' Move to the next row
    Loop
End Sub

The code assumes the workbook containing the macro has a worksheet named Data, and that the list has a blank cell after its final entry. It writes to column B, so existing values there may be replaced. If the list has no blank before the bottom of the worksheet, the row number will eventually go out of range and cause an error. For production code, set a sensible boundary or determine the final row first instead of relying on an unknown blank.

Also decide whether a blank should end the entire scan. This pattern stops at the first gap, even if there is more data lower down. If gaps are allowed, loop through a known row range and handle blank cells inside the loop rather than using the first blank as the terminator.

Choosing between Do Until, Do While, and For

Do Until condition and Do While Not condition can express the same continuation rule, but Until often reads more naturally when the requirement is stated as a stopping point. For example, “repeat until the status is Done” maps directly to an Until condition. Use While when the requirement is more naturally “keep going while there are rows left.”

A For loop is usually clearer when you know the number or range of repetitions in advance, such as processing rows 2 through 100. A For Each loop is often clearer when processing every cell in a specified range. Do Until is useful when the number of passes is not known beforehand and an event or changing value determines when to stop.

  • Use Do Until when a condition describes the point at which repetition should end.
  • Use Do While when a condition describes what must remain true for repetition to continue.
  • Use For for a known sequence of numbers or a fixed number of passes.
  • Use For Each to visit each object in a collection or each cell in a range.

VBA also has the older While...Wend form, but Do loops are more flexible because they support both pre-test and post-test conditions and work with Exit Do.

Common mistakes and safe ways to recover

The most common problem is an infinite loop: the condition never becomes true. Check that a counter is incremented, a row number advances, or the value being tested is changed by the loop or by another process. A second common mistake is placing the update after a statement that exits or errors, so it never runs.

Another frequent error is choosing the wrong test position. A pre-test loop can run zero times; a post-test loop cannot. If the body must execute once, use the post-test form. If it must execute only when the initial condition permits it, use the pre-test form.

VBA does not automatically pause to let a user edit a cell while a macro is running. A loop that repeatedly checks a cell in a tight cycle can consume processing time and make Excel seem unresponsive. For a user-driven pause, a different design is usually better than a continuous polling loop. If a loop must wait for an external process, include a timeout or another defined escape route, and avoid assuming the external process will always finish.

Exit Do provides an immediate way to leave a loop when a separate condition occurs, such as an error flag or a user-selected stop. Use it deliberately and keep the exit condition understandable; otherwise, readers may have difficulty seeing all the ways the loop can end. During debugging, press Ctrl+Break on Windows to interrupt a running macro when available. On Mac, the interrupt key combination can depend on the keyboard and Excel version; the Visual Basic Editor’s Run menu also provides a Reset command to stop code.

Running VBA in Excel and protecting your workbook

In desktop Excel for Microsoft 365 on Windows, open the Visual Basic Editor from Developer > Visual Basic, or press Alt+F11. If Developer is not visible, enable it through File > Options > Customize Ribbon and select Developer. On a Mac, use Developer > Visual Basic; Option+F11 may open the editor, although some keyboards require the Fn key as well. Excel for the web does not run VBA macros, so open the workbook in the desktop app to use this code.

Before running a macro that changes cells, save a separate copy of the workbook. VBA actions cannot be undone with Ctrl+Z, and a macro can overwrite or clear data without the usual worksheet undo history. Check that the workbook, sheet name, starting row, destination column, and stopping condition match your file before running the example. A workbook with macros should be saved in a macro-enabled format such as .xlsm if you need to retain its VBA project.

To run a procedure from the Visual Basic Editor, place the cursor inside the procedure and choose Run Sub/UserForm, or return to Excel and use Developer > Macros to select it. Macro security settings may block code until the file is trusted or macros are enabled under your organization’s rules; do not enable macros in files from an unknown source.

Frequently Asked Questions

What does Do Until mean in VBA?
It means repeat the loop body while its stopping condition is false, and stop when that condition becomes true. The condition can be tested before or after the body.

Does Do Until run at least once?
Not always. Do Until condition ... Loop tests first and may run zero times. Do ... Loop Until condition tests last and always runs the body once.

How do I stop an infinite Do Until loop?
Interrupt the running macro with Ctrl+Break on Windows if available, or use the Visual Basic Editor’s Reset command. Then fix the loop so a value changes toward the stopping condition, and consider adding a limit or timeout.

Can I use Exit Do inside a Do Until loop?
Yes. Exit Do ends the current Do loop immediately, even if its Until condition is still false. It is useful for a separate stop condition, but should not replace a clear normal exit rule.

Is Do Until the same as Do While Not?
They can be logically equivalent when they test the same Boolean condition: Do Until stops when the condition is true, while Do While Not continues while that condition is false. Choose the wording that makes the intended stop or continuation rule easiest to understand.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.