VBA has no break statement like other languages. Where you would type break in C-style code, you use Exit For, Exit Do, or Exit Sub inside an If block. The If decides when to leave; the Exit keyword decides what to leave. Nothing needs installing: the VBA editor is already built into Excel, so all you need is a workbook saved as .xlsm or .xlsb.
What You'll Need
- Excel with macros enabled (any desktop version on Windows or Mac).
- A macro-enabled workbook (.xlsm), because .xlsx discards code.
- Access to the VBA editor. On Windows press
Alt+F11; on Mac open Tools > Macro > Visual Basic Editor (shortcuts differ slightly by version). - If macros are blocked by policy, you will need the workbook in a trusted location and macro settings allowing them. Menu and dialog labels vary a little by Excel version, device and region.
Step-by-Step
- Open the editor and create a module. Press
Alt+F11, then in the Project window right-click your workbook > Insert > Module. All the code below goes in that module. - Start with the simplest exit: bail out of the whole procedure. The most common reason a beginner looks for "if break" is "stop running if a cell is empty." Use
Exit Sub:
Everything after theSub CheckValue() If Range("A1").Value = "" Then MsgBox "A1 is empty - stopping." Exit Sub End If MsgBox "A1 contains " & Range("A1").Value End SubEnd Ifis skipped when the condition is true. UseExit Functionin a Function procedure instead ofExit Sub. - Exit a
Forloop withExit For. This searches a column and stops at the first match, which is the real "break" equivalent:Sub FindFirst() Dim i As Long For i = 1 To 1000 If Cells(i, 1).Value = "Target" Then MsgBox "Found in row " & i Exit For End If Next i End SubExit Forexits only the innermostForthat contains it, not every loop in the procedure. - Exit a
Doloop withExit Do. Same pattern forDo WhileandDo Untilloops:Do While Not IsEmpty(Cells(i, 2)) If Cells(i, 2).Value < 0 Then Exit Do i = i + 1 Loop - Skip one item without leaving the loop. If you only want to ignore a row, use
GoTotheNextline, or better, restructure with anIfblock that simply does nothing when the test fails. A clean version is often shorter than a jump:For i = 2 To lastRow If Not IsEmpty(Cells(i, 1)) And Cells(i, 1).Value <> "" Then ' process the row here End If Next i - Leave an outer loop from an inner loop. There is no direct "exit both" command. Set a Boolean flag, test it after
Next, and break again:Dim done As Boolean For r = 1 To 100 For c = 1 To 50 If Cells(r, c).Value = "Stop" Then done = True Exit For End If Next c If done Then Exit For Next r - Break out of a
For Eachloop the same way.Exit Forworks identically when iterating a collection:Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets If ws.Name = "Backup" Then Exit For Next ws - Test it safely. Save first, run the macro with
F5, and watch the result. To step through line by line, put the cursor in the sub and pressF8; the highlighted line shows exactly where execution actually stops, which is the fastest way to confirm yourIffired at all. - Save as macro-enabled. Use File > Save As > Excel Macro-Enabled Workbook (.xlsm), then close and reopen to confirm the code survived. If it disappeared, you saved over a .xlsx or the workbook came from a blocked source.
Common Mistakes
- Typing
break,continueorExit Loop. None of these exist in VBA.Exit Loopdoes not compile. UseExit For,Exit Do,Exit SuborExit Function. - Putting the exit in the wrong place.
Exit Forplaced betweenForand the code that matters is legal but useless; the loop stops before doing anything. Place it inside theIfblock, after the action you want. - Forgetting the loop never runs at all. If the start value already fails the test (for example
For i = 10 To 1withoutStep -1), nothing inside runs, so your break appears to "work" when actually the loop was empty. - Assuming
Exit Forexits every loop. It exits one level only. Nested loops need the flag pattern above. - Leaving the loop variable undefined. After the loop,
iholds the last value it reached. If you need to know whether you broke early or ran to the end, use a separate Boolean or check whether the found row is empty. - Using
Endinstead ofExit Sub. The bareEndstatement terminates the whole macro abruptly and resets module-level variables, which is rarely what you want. - Leaving orbits and events on. If you set
Application.ScreenUpdating = FalseorEnableEvents = Falseearly, anExit Subjumps over the cleanup lines at the bottom. Reset them before every exit point, or use a single exit label.
Tips
- Aim for one exit point per procedure where practical: accumulate a result, then
Exit Subonce at the end. It is easier to read and keeps cleanup reliable. - For searching a range, the built-in
Range.Findmethod is faster than a manual loop and returnsNothingwhen there is no match, so you can testIf Not found Is Nothingand skip the loop entirely. - Name your flag something meaningful (
stopSearch,foundDuplicate) rather than a genericflag, so the second break is self-explanatory. - Keep the exit condition as the first check in the loop, not buried after heavy work. That saves time on long lists.
- If you need to process every match rather than just the first, remove the
Exit Forand collect results in a variable or on another sheet. - Use
Option Explicitat the top of the module so undeclared variables (a common cause of loops that never break) produce a clear compile error.
Troubleshooting
The macro does not stop when it should. Check that the value really matches: trailing spaces, numbers stored as text, and case differences all break comparisons. Compare with Trim(Cells(i,1).Value) = "Target", and remember VBA string comparison ignores case by default but number-versus-text comparison does not.
You get "Compile error: Expected: expression" or a highlighted Exit line. The keyword is incomplete or in the wrong procedure type. Exit For is only valid inside a For or For Each; Exit Do only inside a Do loop; Exit Sub only inside a Sub.
Excel freezes instead of exiting. Your loop never reaches the test, usually because the counter is not advanced inside a Do loop. Press Esc or Ctrl+Break to interrupt, then make sure i = i + 1 executes on every path.
The code vanished after saving. The file was saved as .xlsx, a format that cannot store macros. Save a copy as .xlsm and re-add the module if needed.
Macros are disabled entirely. Check the Trust Center macro settings and whether the folder is trusted. Do not download code from unknown sites to "fix" it; write or paste only code you understand.
It works on one sheet but not another. Unqualified Cells and Range refer to the active sheet, so select the intended sheet first or qualify them as Worksheets("Data").Range(...).
Frequently Asked Questions
Does VBA have a break statement?
No. The equivalents are Exit For, Exit Do, Exit Sub and Exit Function, usually placed inside an If block.
How do I exit a loop when a condition is met?
Put the test at the top of the loop and call Exit For or Exit Do inside the If. The rest of that iteration and all later ones are skipped.
How can I skip to the next iteration instead of exiting?
VBA has no Continue. Wrap the body in an If that only runs when the condition is not met, or use GoTo a label placed just before Next.
Can I break out of two nested loops at once?
Not directly. Set a Boolean flag in the inner loop, Exit For, then test the flag after Next and break the outer loop.
What is the difference between Exit Sub and End?
Exit Sub leaves the current procedure cleanly and returns control to whatever called it. End halts the entire macro immediately and clears module-level variables.
Why does my loop keep running forever?
The exit test is never true, or the counter is not incremented. Check for extra spaces and text-versus-number mismatches, and confirm the increment line runs on every pass.