Excel VBA Compile Error: Next Without For Explained

VBA & macros · Updated November 19, 2026

In Excel VBA, “Next without For” means VBA found a Next statement that it could not match to an open For or For Each loop. Find the unmatched Next, then check upward for a missing, commented-out, misplaced, or mismatched loop opener; correct the loop structure and compile again.

What the error means

For and For Each start loops in VBA, and Next ends them. VBA checks that these blocks are properly paired before it can compile the procedure. If it reaches a Next with no corresponding loop still open, it stops with the compile error “Next without For.” It is a code-structure problem, not a problem with the values in the worksheet or the result of a calculation.

The editor may highlight the Next line, even when the actual mistake is several lines above it. For example, a loop opener might have been deleted while the closing line remained, or a For line might have been turned into a comment. The highlighted line is where VBA detected the mismatch; it is not necessarily where the mistake began.

A For loop normally repeats a known range of numeric values. A For Each loop visits the members of a collection or array. Both require a matching Next. Other control structures, such as If and Select Case, have their own closing statements and cannot substitute for a loop’s Next.

How For and Next blocks are matched

A basic loop has one opener and one closer. The following example is a complete procedure that writes the numbers 1 through 5 into cells A1 through A5 on the active worksheet:

Sub FillNumbers()
    Dim i As Long

    ' Write one number in each of five cells.
    For i = 1 To 5
        Cells(i, 1).Value = i
    Next i
End Sub

In this example, For i = 1 To 5 opens the loop and Next i closes it. The counter name after Next is optional, but including it makes the intended pairing easier to see. VBA also allows a loop without an explicit counter on the closing line, as in Next.

Nested loops must close in reverse order from the order in which they open. In the next procedure, the inner loop finishes before the outer loop:

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

    ' Fill a five-row by three-column area.
    For rowNum = 1 To 5
        For colNum = 1 To 3
            Cells(rowNum, colNum).Value = rowNum * colNum
        Next colNum
    Next rowNum
End Sub

The first Next belongs to the most recently opened loop, For colNum. The second belongs to For rowNum. Reversing that order, omitting a closing line, or accidentally adding an extra Next can leave VBA unable to match the blocks. When investigating an error, indent each loop body consistently: the indentation often makes an incorrect nesting order obvious.

Common causes and the clues they leave

The most frequent cause is an extra Next or a missing For. This can happen during editing when someone copies a block, deletes part of a loop, or moves code without moving its closing line. A related case is a loop opener commented out with an apostrophe while its Next remains active. VBA ignores the commented line, so it sees a closer without an opener.

Other likely causes include:

  • A loop was deleted incompletely. Search upward in the same procedure for each For and For Each, and count the corresponding Next statements.
  • A loop is closed too early. A misplaced Next can make a later closing line appear unmatched. Check the order of nested loops, not just the total number of openers and closers.
  • A closing line was pasted into the wrong procedure. A procedure boundary such as End Sub or End Function ends the procedure; a loop cannot be opened in one procedure and closed in another.
  • The wrong construct was used to close a block. End If closes an If block, not a For loop. Conversely, adding an End If does not repair a missing Next.
  • Code was changed by commenting or conditional compilation. If the active version of the code contains a Next but not its opener, the structure is incomplete. Check the code that is actually active, including any compiler directives.

Do not assume every loop-related error has this exact wording. A counter mismatch or another structural issue may produce a different compile message. Read the diagnostic, inspect the highlighted line, and then review the surrounding procedure rather than changing loop variable names at random.

A reliable way to find and fix it

  1. Open the Visual Basic Editor. In Excel for Microsoft 365 on Windows, press Alt+F11, or select Developer > Visual Basic. On a Mac, use Option+F11; depending on the keyboard and system settings, you may also need to hold Fn.
  2. Go to the reported procedure. Double-click the relevant module or object in the Project Explorer, then find the highlighted Next. If the Project Explorer is hidden, use View > Project Explorer in the Visual Basic Editor.
  3. Inspect upward within the same procedure. Look for the nearest unmatched For or For Each. Check whether it is commented out, misspelled, or separated from its body by code that indicates the block was edited incorrectly.
  4. Trace nested loops from the inside out. Pair the last loop opened with the next appropriate closing line, then continue outward. Indent the code consistently or temporarily add blank lines to make each loop body easier to follow.
  5. Make the smallest structural correction. Restore a genuinely missing opener, remove an accidental extra closer, or move a closing line to the correct position. Do not add a new For just to silence the message: that can create a different logic error.
  6. Compile again. In the Visual Basic Editor, select Debug > Compile VBAProject. If another error appears, address that one and compile again; VBA may reveal additional problems one at a time.

Compilation checks the VBA project’s code structure; it does not confirm that the macro will produce the intended worksheet results. After it compiles, review the procedure’s logic and test it on suitable data. Save a copy of the workbook before running a macro: macro actions cannot be undone with Ctrl+Z. On Mac, the same limitation applies; make a backup before running code there too.

Examples of broken loop structure

This deliberately broken example has a Next with no corresponding For. It illustrates the error; do not run it as a working macro.

Sub BrokenExample()
    Dim i As Long

    ' This Next has no matching For.
    Next i
End Sub

More often, the opener is present but has been commented out. In the example below, the apostrophe makes the For line a comment, so VBA does not treat it as an active loop:

Sub CommentedOpener()
    Dim i As Long

    ' The apostrophe disables the loop opener.
    ' For i = 1 To 3
        Debug.Print i
    Next i
End Sub

To correct that structure, restore the active For line and make sure the statements inside the loop are appropriate. For instance, the following complete procedure prints the values 1, 2, and 3 in the Immediate window:

Sub PrintNumbers()
    Dim i As Long

    ' Print three values in the Immediate window.
    For i = 1 To 3
        Debug.Print i
    Next i
End Sub

For procedures that change workbook data, save a copy before running them because Ctrl+Z cannot reverse macro actions. The examples above that write to cells also modify the active worksheet, so check which workbook and sheet are active before running a similar procedure.

For Each loops and nested blocks

A For Each loop also ends with Next. Its control variable must be suitable for the items being visited: it is typically a Variant or an Object variable. Here is a complete example that visits each worksheet in the active workbook and prints its name:

Sub ListSheetNames()
    Dim ws As Worksheet

    ' Visit each worksheet in the active workbook.
    For Each ws In ActiveWorkbook.Worksheets
        Debug.Print ws.Name
    Next ws
End Sub

If you accidentally delete the For Each ws In ... line but leave Next ws, the same unmatched-closer problem applies. A For Each loop can also be nested inside a numeric For loop, but each one still needs its own matching closing statement in reverse nesting order.

It is valid to put an If block inside a loop. Close the If block with End If, then close the loop with Next. Keep the two structures visually distinct and correctly nested. A conditional statement does not cancel or replace a loop, even if its condition determines whether a particular statement runs.

Debugging tips and prevention

The Visual Basic Editor’s compile command is useful after a repair because it checks the project for compile-time problems without requiring you to run the macro. If the compile command is unavailable, the project may already be compiled with no pending edits, or the editor may be in a state where compilation is not currently applicable. Make a code change or inspect the current error state rather than assuming that a disabled command proves the procedure’s behavior is correct.

These habits reduce the chance of introducing a mismatched loop:

  • Indent as you type. Indent the body one level and return to the previous level for each Next. VBA’s automatic indentation can help, but it does not verify the logic.
  • Name the counter on Next. Although optional, Next rowNum is easier to review than a bare Next in a procedure with several loops.
  • Edit complete blocks together. When removing or moving a loop, include both its opener and closer and review any nested loops inside it.
  • Compile after meaningful edits. Frequent checks make it easier to associate a new error with the recent change instead of searching through a large module later.
  • Use the editor’s search carefully. Searching for For or Next can help, but comments and text inside strings may also contain those words. Confirm that each match is actual VBA code.

Changing Next i to simply Next is not a general fix. The counter name on Next is optional, but removing it does not create a missing loop opener or repair incorrectly nested blocks. Likewise, adding another For line without understanding the intended work can make a program compile while causing it to repeat the wrong statements.

Excel version and platform considerations

The loop syntax and the meaning of this compile error are longstanding VBA concepts, and the troubleshooting approach applies to desktop Excel for Microsoft 365 on Windows and Mac. The Visual Basic Editor’s menus or keyboard shortcuts may differ slightly by platform, but the structural requirement remains: every active For or For Each block must be correctly closed in its procedure.

Excel for the web does not run VBA macros or provide the desktop Visual Basic Editor for editing VBA code. If a workbook produces this compile error, open it in the Excel desktop application to inspect and compile the VBA project. A workbook can still contain macros when viewed online, but that does not mean those macros can be run or repaired in the browser.

Frequently Asked Questions

Why does VBA highlight Next instead of the missing For?
VBA reports the problem when it reaches a Next that has no loop opener available to match. The missing or disabled For may be earlier in the procedure, so inspect the code above the highlighted line and check the nesting.

Can I write Next without a counter name?
Yes. VBA permits Next without naming the loop counter. Writing Next i or Next ws is often clearer, especially with nested loops, but it cannot repair a missing opener.

Does End If close a For loop?
No. End If closes an If block, while Next closes a For or For Each loop. A procedure can contain both structures, but each must have its own correct closing statement.

Why do I get the error after commenting out a line?
If the line you commented out was the loop opener, VBA no longer sees it as executable code. An active Next left below it then has no matching For. Restore the opener or remove the obsolete closer, depending on the intended code.

Can I fix this error in Excel for the web?
No. Excel for the web does not support editing or running VBA macros. Open the workbook in desktop Excel, use the Visual Basic Editor to repair the loop, and compile the project there.

Will compiling run my macro?
No. Debug > Compile VBAProject checks the VBA project for compile errors; it does not run the procedure. Running a macro is a separate action, and changes made by a macro cannot be reversed with Ctrl+Z, so save a backup first.

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.