Excel VBA Next Without For Error: Causes and Fixes

VBA & macros · Updated November 19, 2026

The Excel VBA compile error “Next without For” means VBA has reached a Next statement without finding a matching, currently open For loop. Check the code above the highlighted line for a missing, commented-out, misplaced, or incorrectly nested For; then use the Visual Basic Editor’s Debug > Compile VBAProject command to check the repair.

What the error means

A For loop repeats a block of code, and its matching Next marks the end of that block and advances the loop counter. VBA must be able to pair each loop start with an end statement in the correct order. If it cannot, compilation stops before the macro runs. The problem is therefore usually in the loop structure, not in a value in a worksheet cell.

The error message often points at the Next VBA cannot pair, but that line may be perfectly ordinary. The actual cause can be several lines earlier: perhaps a For line was deleted, turned into a comment, or placed inside a different block than intended. Start investigating at the highlighted line, then read upward through the procedure to find the unmatched structure.

How For and Next are paired

A basic loop opens with For and closes with Next. You can include the counter variable after Next to make the pairing easier to read. This example adds the values in B2:B5 on the active worksheet:

Sub AddValues()
    Dim rowNum As Long
    Dim total As Double

    total = 0
    ' Assumes the values to add are in B2:B5 on the active sheet.
    For rowNum = 2 To 5
        total = total + Cells(rowNum, 2).Value
    Next rowNum

    MsgBox total
End Sub

There are four iterations, for rows 2, 3, 4, and 5. The procedure displays their sum. A loop can also count downward with Step -1, or use a different positive step, but it still needs a matching Next. The counter name after Next is optional in VBA; leaving it off does not, by itself, cause this error.

For nested loops, close the most recently opened loop first. This is the same inside-out order used for nested brackets: the inner loop must finish before the outer loop does.

Sub VisitCells()
    Dim rowNum As Long
    Dim colNum As Long

    ' Visits cells in a two-row by three-column area.
    For rowNum = 1 To 2
        For colNum = 1 To 3
            Cells(rowNum, colNum).Value = rowNum * colNum
        Next colNum
    Next rowNum
End Sub

Before running either example, save a copy of the workbook. VBA macros cannot be undone with Ctrl+Z, so a copy gives you a way back if a macro changes data unexpectedly.

Common causes to check first

  • The opening loop is missing. A Next may remain after someone removed or moved its corresponding For line.
  • The opening line is commented out. For example, putting an apostrophe before a For line makes VBA treat the rest of that line as a comment. A later Next is then left without an open loop.
  • A loop was edited or pasted incompletely. Copying only part of a procedure, or deleting a block while refactoring, can leave an unmatched loop boundary.
  • Nested loops close in the wrong order. If an inner loop is still open when VBA reaches the outer loop’s Next, the structure is not properly paired. Review every opening and closing line from the inside out.
  • A surrounding block is malformed. A missing or extra If, End If, or other block boundary can make the intended loop structure confusing or invalid. The compiler may report a related structural error instead, so read the complete procedure rather than changing only the highlighted line.

A mismatched counter name is worth checking for readability, especially in nested loops. However, the name written after Next is optional, and a counter-name issue can produce a different compile message depending on the code. Do not assume that changing the name alone will fix an unmatched loop.

A practical way to find and fix it

  1. Open the Visual Basic Editor. In Excel for Microsoft 365 on Windows, press Alt+F11. On a Mac, the editor can be opened with Option+F11; depending on the keyboard settings, you may also need Fn. You can also use the Visual Basic command on Excel’s Developer tab if it is available.
  2. Locate the reported procedure. When the compile message appears, note the procedure and the line VBA highlights. The highlighted Next is a useful starting point, not proof that it is the line you should delete.
  3. Inspect upward from that line. Count the open For loops and their corresponding Next statements. For nested loops, pair the innermost opening line with the first applicable closing line, then work outward.
  4. Check comments and edits. Look for an apostrophe before a loop start, a partly pasted block, or a loop boundary that was moved during editing. Restore the missing structure or remove a genuinely obsolete closing statement, according to the intended logic.
  5. Compile again. In the Visual Basic Editor, choose Debug > Compile VBAProject. Correct the first reported error, then compile again; later problems may not become visible until the first one is fixed.

Do not add a new For line simply to silence the message. An invented loop may compile but repeat code incorrectly, change the wrong cells, or never finish as intended. Determine what the macro is meant to repeat, then make the loop boundaries match that task.

Nested loops and other block statements

When loops are nested, indentation is a quick visual check. Every inner For should have its own Next before the outer loop reaches its closing Next. Writing the counter name after each closing statement makes this structure clearer, even though VBA permits the name to be omitted.

VBA also has other block structures, including Do loops and If blocks. They use their own closing statements, such as Loop and End If; these do not substitute for a Next. If editing one of these blocks appears to cause the error, check that each structure is properly opened and closed and that the loop has not accidentally been split across an incompatible arrangement of blocks.

For example, an If condition can control whether code inside a loop runs, while the Next still closes the loop. The key is to keep the block boundaries in the right order. A missing End If can cause a compile error of its own or make the reported location less helpful, so fix the structure VBA reports rather than guessing from one line.

Why the error appears before the macro runs

VBA checks the procedure’s syntax and block structure during compilation. A macro with an unmatched For and Next cannot start executing as normal, so changing a worksheet value or pressing Run repeatedly will not repair the source code. Fix the structure in the Visual Basic Editor first.

Compilation errors are different from runtime errors. A runtime error occurs while code is executing—for example, when a statement tries to use an invalid object. “Next without For” is a compile-time structural error. It is also different from a loop that runs the wrong number of times: that loop may compile successfully but still need a logic correction.

Ways to prevent the problem

  • Indent the statements inside each loop and align each Next with its corresponding For.
  • Include the counter name after Next, particularly in nested loops, so the intended pairing is visible to a person reading the code.
  • When deleting or moving a loop, edit its opening and closing statements together, then compile before making more changes.
  • Compile after substantial edits instead of waiting until the macro is finished. This helps identify the first structural mistake while the recent change is still easy to inspect.
  • Keep a backup before running macros that change workbook contents; Ctrl+Z does not undo VBA macro actions.

These habits do not change VBA’s rules, but they make an unmatched boundary easier to spot. For a long procedure, temporarily reviewing one loop at a time is often more reliable than scanning the entire module at once.

Frequently Asked Questions

Why does VBA highlight Next when the mistake is above it?
VBA reports the point where it can no longer match the loop structure. The Next may be valid on its own; a missing, commented, or misplaced For earlier in the procedure may be the real cause.

Can I leave out the variable after Next?
Yes. VBA allows a closing Next without a counter name. Including the name is a useful readability practice, but adding or removing it does not replace a missing loop start.

Does Next without For mean my loop has run too many times?
No. It is a compile error, so the procedure cannot run normally until the code structure is corrected. A loop that runs too many times is a separate logic problem in code that compiles.

What is the difference between For without Next and Next without For?
“For without Next” generally means VBA found an opening loop that was not closed. “Next without For” generally means it found a closing statement without a matching open loop. In either case, inspect the whole loop and any nested blocks.

Will restarting Excel fix the error?
No. The error is in the VBA code, not a temporary worksheet state. Correct the loop in the Visual Basic Editor and compile the project again.

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.