VBA Loop Without Do: Choosing For, While, or For Each

VBA & macros · Updated November 2, 2026

VBA Loop Without Do: Choosing For, While, or For Each

Yes. In VBA, you can loop without using Do: use For...Next for a known number of repetitions, For Each...Next to visit items in a collection or range, or the older While...Wend structure to repeat while a condition is true. Choose the form that matches how the loop should end; for condition-driven loops that need a clear exit statement, Do is often safer and easier to maintain.

What a loop without Do means

A loop repeats a block of VBA statements. Do is one family of loop syntax, not a requirement for repetition. VBA also supports counter-based loops, collection loops, and the While...Wend structure. These options cover most situations in which someone asks how to write a VBA loop without Do.

The important distinction is not simply the keywords. It is the rule that controls when the loop stops. A For...Next loop normally stops after its counter reaches a specified limit. A For Each...Next loop stops after it has visited every item. A While...Wend loop checks a condition and stops when that condition becomes false. The wrong choice can make code harder to read or, in the case of a condition that never changes, leave Excel apparently stuck in an endless loop.

These are VBA language constructs, not worksheet formulas. In Excel for Microsoft 365 on Windows, put macro code in a standard VBA module, commonly opened with Alt+F11 and inserted using Insert > Module in the Visual Basic Editor. On Mac, the Visual Basic Editor can also be opened with Option+F11, though some keyboards require the Fn key. Before running a macro that changes a workbook, save a copy: macro changes cannot be undone with Ctrl+Z.

Choose the loop by its stopping rule

Start by describing the stopping rule in plain language. If you know the number of repetitions, count them with For...Next. If you need to process each worksheet, cell, or other collection member, use For Each...Next. If you must continue until a changing condition is no longer true and specifically want no Do keyword, While...Wend is available. If the stopping point depends on a condition but you also need to break out early, consider whether Do While or Do Until would make the intent clearer.

  • Known count: repeat a fixed number of times or move through a numeric range with For...Next.
  • Every member: process each item in a range or collection with For Each...Next.
  • Condition stays true: use While...Wend if avoiding Do is a firm requirement, and make sure the loop changes the condition.
  • Condition plus early exit: a Do loop is usually the more flexible option because VBA provides Exit Do.

Choosing a loop for readability matters as much as choosing one that runs. A future reader should be able to tell whether the code is counting, traversing a set of objects, or waiting for a condition to change.

Use For…Next for a known number of repetitions

For...Next is the most direct replacement when the number of repetitions is known or can be expressed as a counter range. Declare the counter, give it a starting value and an ending value, and close the block with Next. An optional Step sets the amount added to the counter each time. Without it, the step is 1.

For example, with values in A2:A6 and B2:B6 on the active worksheet, this macro writes each row’s product to column C. It processes five rows, so a count-controlled loop is a natural fit:

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

    Set ws = ActiveSheet

    ' Write each row's product in column C.
    For rowNum = 2 To 6
        ws.Cells(rowNum, 3).Value = ws.Cells(rowNum, 1).Value * ws.Cells(rowNum, 2).Value
    Next rowNum
End Sub

Before running it, save a copy of the workbook because this macro writes to cells and macro actions cannot be undone with Ctrl+Z. The example assumes the input cells contain numbers; blank or text inputs may produce results that are not what the workbook needs.

For an ascending count, the start is normally less than or equal to the end. To count down, use a negative step, such as For i = 10 To 1 Step -1. If the counter begins beyond the limit in the direction of travel, the loop may run zero times. Use a Long for ordinary row numbers and counters in Excel; an Integer has a much smaller range and is generally not a good choice for worksheet row indexes.

A For loop can also stop early with Exit For. This is useful when a search finds a match before reaching the end. Use it deliberately: if the loop should always inspect every item, an early exit changes the result. In nested loops, Exit For exits only the innermost loop that contains it.

Use While…Wend when a condition controls repetition

The syntax without Do for a condition-controlled loop is While condition, followed by the statements to repeat and then Wend. VBA checks the condition before each pass. If it is false at the first check, the body runs zero times. If it remains true, the body runs again.

For example, this macro adds the integers from 1 through 5. The condition is n <= 5, and the body increases n on every pass. The final value of total is 15, because 1 + 2 + 3 + 4 + 5 equals 15:

Sub SumFiveNumbers()
    Dim n As Long
    Dim total As Long

    n = 1
    total = 0

    ' Add the current number, then advance toward the stopping point.
    While n <= 5
        total = total + n
        n = n + 1
    Wend

    Debug.Print total
End Sub

Before running a macro in a workbook that it may change, save a copy; Ctrl+Z cannot reverse macro actions. This particular example only prints the result in the Immediate window, which you can show in the Visual Basic Editor with Ctrl+G on Windows. On Mac, use the Visual Basic Editor’s View menu to display the Immediate window if the shortcut differs on your keyboard.

The essential safety check is that something in the loop must eventually make the condition false. If the increment n = n + 1 were omitted, n would stay at 1 and the condition would remain true forever. Unlike a Do loop, While...Wend does not provide an Exit While statement. That limitation is one reason many VBA programmers prefer Do While when they need condition-driven repetition with a separate early-exit path.

Use For Each to process every item

When the task is to visit each member of a set, For Each...Next expresses that purpose more clearly than a numeric counter. A worksheet range is a collection of cells, so you can process its cells without calculating row numbers yourself. For example, this macro checks A2:A20 on the active worksheet and highlights negative numeric values in yellow:

Sub HighlightNegativeValues()
    Dim cell As Range

    ' Check each cell in the selected address range.
    For Each cell In ActiveSheet.Range("A2:A20")
        If IsNumeric(cell.Value) Then
            If cell.Value < 0 Then
                cell.Interior.Color = vbYellow
            End If
        End If
    Next cell
End Sub

Save a copy before running it because the macro changes cell formatting, and Ctrl+Z cannot undo a macro. The numeric check prevents text in the range from being treated as a number. This example only examines the stated range; it does not automatically expand to include additional rows.

In a For Each loop, the control variable must be suitable for the items being visited. A Range variable is appropriate for cells, while a variable declared as Worksheet can represent worksheets in a workbook’s Worksheets collection. For arrays, a Variant control variable is commonly used. Unlike a counter loop, the code does not normally set or increment the control variable itself; VBA advances to the next item.

Common mistakes and how to avoid them

Most loop problems come from a mismatch between the stopping rule and the code that updates it. Before running a macro, check the initial value, the condition, and the statement that moves the loop toward completion. For a counter loop, check that the step points toward the limit. For a condition loop, check that each pass can change the condition. For a collection loop, check that the selected collection is the one you intend to process.

  • Infinite While loop: the condition remains true because the body never updates the relevant variable. Add a progress step or use a bounded counter.
  • Wrong direction in For: a positive step is used when counting down, or vice versa. Match the sign of Step to the direction of travel.
  • Off-by-one range: the start or end row excludes a desired cell or includes a header. Verify the actual first and last cells before running.
  • Unexpected edits: a macro relies on ActiveSheet, but another sheet is active. Qualify the worksheet explicitly when the target must not depend on selection.
  • Trying to exit While…Wend: VBA has no Exit While. Use a condition that can turn false, restructure with For...Next, or choose a Do loop when an explicit exit is needed.

For larger ranges, repeatedly reading and writing individual cells can be slower than working with a VBA array, although a simple loop is often easier to understand. First make the stopping logic correct and test on a copy or a small sample. Performance changes are useful only if they preserve the intended result.

When a Do loop is still the better choice

Avoiding Do is possible, but there is no general rule that says it should be avoided. A Do While...Loop or Do Until...Loop can test a condition before the body or, with the condition placed after the body, test it after a pass. That flexibility is useful when the body must run at least once, or when an explicit Exit Do is needed after checking a second condition.

For example, searching through an unknown number of records until a blank cell appears is naturally condition-driven. You could use While...Wend if the loop updates its row counter and the blank-cell condition is enough to stop it. If the search also needs to stop when it finds a match, an explicit exit can make that second stopping reason easier to express with Do. The goal is not to remove a keyword at any cost; it is to make every way the loop can finish visible and reliable.

Frequently Asked Questions

Can VBA loop without Do?
Yes. Use For...Next, For Each...Next, or While...Wend. Select the structure according to whether you are counting repetitions, visiting items, or repeating while a condition remains true.

What is the VBA alternative to Do While?
While condition ... Wend repeats while the condition is true and does not use the Do keyword. If you also need an explicit early exit, VBA’s Do While...Loop is usually more convenient because it supports Exit Do.

Can I use Exit For to stop a VBA loop early?
Yes. Put Exit For inside a For...Next or For Each...Next loop when a condition means there is no reason to continue. It exits the innermost active For loop, not every enclosing loop.

Does While…Wend run at least once?
No. VBA evaluates the condition before entering the loop body. If the condition is false at that first check, the body runs zero times. Use a post-test loop when the body must run once before the condition is checked.

Why does my VBA loop never stop?
Usually, its stopping condition never becomes false. Check that the loop updates the counter or value used in the condition, and confirm that the update moves toward the limit. Test with a small range or a bounded counter before running it on a full workbook.

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.