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

VBA Exit For in Excel — Exit For, Exit Do, Exit Sub and the Clean Early Exit

|

VBA Exit For in Excel — Exit For, Exit Do, Exit Sub and the Clean Early Exit

TL;DR — Exit For leaves a For loop immediately; Exit Do leaves a Do loop; Exit Sub and Exit Function return from a procedure. They are one idea — leave the current structure now — and the code after that structure keeps running. The trap: Exit For only leaves the innermost loop, so in nested loops the outer one carries on. VBA has no Continue and no Exit If; to skip one iteration you use an If (or, rarely, a GoTo a label just before Next).

Sub FindFirstMatch()
    Dim i As Long, found As Long
    For i = 1 To 100000
        If Cells(i, 1).Value = "TARGET" Then
            found = i
            Exit For              ' stop the moment we find it - no need to scan the rest
        End If
    Next i
    MsgBox IIf(found > 0, "Found on row " & found, "Not found")
End Sub

Exit is how you leave a block early without leaving the whole program. Break out of a loop with Exit For or Exit Do, return from a procedure with Exit Sub, and let the rest of the macro run on. That last part is the whole point, and the one line that separates Exit from End: Exit closes a door; End pulls the plug.

What you'll learn

  • The mental model that ties Exit For, Exit Do, Exit Sub and Exit Function into one rule
  • Why Exit For breaks out of a loop, and why the code after the loop still runs
  • The number-one trap: Exit For only leaves the innermost loop in a nested For
  • Why VBA has no Continue and no Exit If, and how to skip a single iteration cleanly
  • The guard-clause pattern (If ... Then Exit Sub) that replaces deeply nested If blocks
  • How Exit Sub differs from End — leaving a procedure versus stopping the whole macro

The mental model: leave this structure, keep the program running

Every form of Exit names the structure it leaves, and leaves only that structure:

Exit For        ' leave the enclosing For loop
Exit Do         ' leave the enclosing Do loop
Exit Sub        ' leave the enclosing Sub (return to the caller)
Exit Function   ' leave the enclosing Function (return to the caller)

Think of your procedure as a set of nested rooms — a Sub contains a loop, the loop contains an If. Exit For steps out of the loop's room but stays in the Sub. Exit Sub walks out of the whole procedure. In every case, execution resumes at the next thing outside the structure you left — the line after Next, or the caller that invoked your Sub. Nothing after your Exit inside that block runs, but everything after the block does. That is what makes Exit structured: you always know exactly where control lands, because it lands right outside the box you named.

Exit For: stop a loop the moment you are done

A For loop's default is to run every iteration. Very often you do not need every iteration — you are searching, and once you find the answer, the remaining passes are wasted work:

Dim i As Long
For i = 2 To 100000
    If Cells(i, 1).Value = "" Then Exit For   ' first blank row - stop here
Next i
' i now holds the row where we stopped; execution continues from this line

Two things are worth pinning down. First, Exit For does not reset or clear the loop counter — after the loop, i holds whatever value it had when you exited, which is often exactly what you want (the row you stopped on). Second, the loop is the only thing you left. Any code after Next i runs normally. If you meant to abandon the entire procedure, that is Exit Sub, not Exit For.

The number-one trap: Exit For only leaves the innermost loop

This is the mistake that costs people an afternoon. When you nest one For inside another, Exit For leaves only the loop it sits in — the inner one. The outer loop keeps going as if nothing happened:

Dim r As Long, c As Long
For r = 1 To 100
    For c = 1 To 50
        If Cells(r, c).Value = "STOP" Then Exit For   ' leaves the c loop ONLY
    Next c
    ' <-- control lands HERE, and the outer r loop continues to r = 100
Next r

There is no Exit For, For to break two levels at once. When you genuinely need to stop both loops, you have three honest options: set a Boolean flag and test it in the outer loop's condition; move the nested loops into their own Function and use Exit Function to leave both at once; or, the one place seasoned VBA writers still reach for it, GoTo a label just past the outer Next. The Function approach is usually the cleanest — see VBA GoTo for why the jump is a last resort.

Exit Do: the same idea for Do loops

Do loops get their own keyword, Exit Do, for exactly the same reason. It matters most for the Do ... Loop with no condition, which would otherwise run forever:

Dim n As Long
Do
    n = n + 1
    If n > 1000 Then Exit Do          ' the only way out of this loop
Loop

Use Exit Do inside Do While, Do Until, and the bare Do ... Loop. Do not try to use Exit For inside a Do loop or vice versa — the keyword must match the loop type, or VBA raises a compile error. For the difference between the loop forms themselves, see VBA While Loop.

Exit Sub and Exit Function: return early from a procedure

Outside loops, the same idea returns you from a whole procedure. Exit Sub ends a Sub immediately and hands control back to whatever called it; Exit Function does the same for a Function, returning whatever value the function name currently holds:

Function SafeDivide(a As Double, b As Double) As Double
    If b = 0 Then
        SafeDivide = 0
        Exit Function        ' return 0 now; skip the division below
    End If
    SafeDivide = a / b
End Function

Note the order in Exit Function: set the return value before you exit, because Exit Function leaves right away and never reaches the lines below it. This "check a condition, bail out early" shape is the single most useful thing Exit does, and it has a name.

The guard-clause pattern: Exit Sub instead of nested Ifs

When a procedure has preconditions — a sheet must exist, a cell must not be blank, the user must have picked something — the tempting structure is a pyramid of nested If blocks. The flatter, more readable alternative is a guard clause: test each precondition at the top and Exit Sub if it fails, so the real work runs at the left margin with no nesting:

Sub ProcessSelection()
    If Selection Is Nothing Then Exit Sub
    If Selection.Cells.Count = 0 Then Exit Sub
    If Not TypeName(Selection) = "Range" Then Exit Sub

    ' by here, every precondition is satisfied - do the work, un-nested
    Dim cell As Range
    For Each cell In Selection
        cell.Value = UCase(cell.Value)
    Next cell
End Sub

Each guard reads as a plain sentence — "if there is nothing selected, we are done." The payoff is that the important code is never buried three If levels deep. When a procedure has one clear reason to stop early, an Exit Sub guard at the top beats wrapping the whole body in If ... End If.

There is no Continue and no Exit If in VBA

Two things programmers coming from other languages reach for and will not find. VBA has no Continue statement to skip to the next iteration, and no Exit If — Exit only leaves loops and procedures, never an If block (an If ends at its End If on its own). To skip one iteration of a loop, wrap the body in an If so the skip is just "do nothing this time":

For i = 1 To n
    If Cells(i, 1).Value = "" Then
        ' skip blank rows - simply do nothing and fall through to Next
    Else
        ' process non-blank rows here
    End If
Next i

That inverted If is the idiomatic VBA "continue." The alternative you will see in older code — GoTo a label placed just before Next — works, but it is a jump, and jumps are harder to follow than an If. Reach for it only when the skip logic is genuinely too tangled for an If, and see VBA GoTo first.

Exit is not End: a door, not the plug

The most important boundary on this page. Exit Sub returns from the current procedure and lets the program continue — the caller runs its next line, your cleanup runs, Excel is left in a sane state. End stops the entire macro dead: it discards every variable, closes UserForms, and — the real damage — skips any cleanup you had queued, so Application.ScreenUpdating can stay False and events can stay disabled. When you want to leave this procedure, the answer is almost always Exit Sub, never End.

How ExcelMaster helps

Early exit is where "correct" and "fast" meet: a search that stops on the first match instead of scanning 100,000 rows, a procedure that bails out cleanly when its inputs are wrong. The two classic mistakes are expecting Exit For to break out of nested loops and confusing Exit Sub (leave the procedure) with End (stop everything and skip cleanup).

ExcelMaster lets you describe the goal — "find the first row where column A is blank and stop" — and it writes the loop with the right Exit, adds guard clauses for the preconditions, and structures the cleanup so nothing is left half-done. You keep the workbook and the code.

Frequently asked questions

How do I break out of a For loop in VBA?

Use Exit For. It leaves the enclosing For loop immediately and continues at the first line after Next. It is typically placed inside an If so the loop stops only when a condition is met, for example If Cells(i, 1).Value = "TARGET" Then Exit For. The loop counter keeps its current value after you exit, which is often the row or index you were looking for.

Does Exit For break out of nested loops?

No — Exit For leaves only the innermost loop it sits in; the outer loop keeps running. VBA has no way to exit two loop levels with a single statement. To stop both, set a Boolean flag and test it in the outer loop, move the nested loops into a Function and use Exit Function, or GoTo a label placed after the outer Next.

What is the difference between Exit Sub and End?

Exit Sub returns from the current procedure and lets the program continue — the caller resumes, and any cleanup you have runs. End stops the entire macro at once: it wipes all variables, closes forms, and skips your cleanup, which can leave ScreenUpdating off or events disabled. To leave a procedure, use Exit Sub; reserve End for genuine emergencies.

How do I skip an iteration in a VBA loop?

VBA has no Continue statement. The idiomatic way is to wrap the loop body in an If so that the iterations you want to skip simply do nothing and fall through to Next. Older code sometimes uses GoTo a label just before Next to emulate Continue, but an inverted If is clearer and does not introduce a jump.

Is there an Exit Do and an Exit While?

There is Exit Do, which leaves any Do loop (Do While, Do Until, or a bare Do ... Loop). There is no Exit While — the old While ... Wend loop has no exit statement at all, which is one more reason to prefer Do loops. The Exit keyword must match the loop type: use Exit For in For loops and Exit Do in Do loops.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-24.

Related guides: VBA GoTo · VBA End · VBA For Loop · VBA While Loop · VBA On Error