Use Excel VBA's Continue For statement to skip the current iteration of a For loop and jump directly to the next one, without exiting the loop entirely. This is essential for filtering data, skipping errors, or avoiding unnecessary processing in loops—saving time and improving code clarity. No external tools or add-ins are needed; this works in all modern versions of Excel with VBA enabled.
What You'll Need
- Microsoft Excel 2010 or later (64-bit or 32-bit)
- VBA enabled in Excel (File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable VBA macros)
- A worksheet with data you want to process (e.g., a column of numbers, names, or dates)
- Access to the VBA Editor (press
Alt + F11)
Step-by-Step
- Open Excel and press
Alt + F11to open the Visual Basic for Applications editor. - In the Project Explorer (left pane), right-click on your workbook name (e.g.,
VBAProject (YourWorkbook.xlsx)), then selectInsert > Module. A blank code window will appear. - Declare your subroutine by typing:
Sub SkipIterations()and press Enter to auto-generate the closingEnd Sub. - Inside the subroutine, define a loop variable and set a range to loop through. For example:
Dim i As LongandDim rng As Range. - Set the range you want to iterate. For instance:
Set rng = Range("A1:A10"). Adjust the range to match your data. - Start the For loop:
For i = 1 To rng.Cells.Count. - Inside the loop, use
rng.Cells(i).Valueto access the value of the current cell. For example:Dim currentValue As Variantfollowed bycurrentValue = rng.Cells(i).Value. - Add a condition to skip certain iterations. For example, if you want to skip any cell with a value of 0, use:
If currentValue = 0 Then Continue For. - After the
Continue Forstatement, add the code you want to run only when the condition is NOT met. For example:rng.Cells(i).Interior.Color = RGB(255, 255, 0)to highlight non-zero cells yellow. - Close the loop with
Next i. - Optionally, add a message box to confirm completion:
MsgBox "Loop completed. Non-zero cells highlighted." - Save your workbook as a macro-enabled file:
File > Save As > Excel Macro-Enabled Workbook (*.xlsm). - Return to Excel by pressing
Alt + Q, then pressAlt + F8, selectSkipIterations, and clickRun.
Common Mistakes
- Using
Continue Foroutside a For loop—this causes a compile error. It only works inside aFor...Nextblock. - Typing
Continueinstead ofContinue For. The full keyword is required; partial syntax is invalid. - Placing
Continue Forafter other logic that modifies the loop variable (e.g., changingimanually). This breaks the loop’s integrity. - Using
Continue Forin a nested loop without considering which loop it affects—it always applies to the innermost enclosing For loop. - Forgetting to define the loop variable with
Dim, which can cause unexpected behavior if Option Explicit is enabled.
Tips
- Use
Continue Forto avoid nestedIf...Then...Elseblocks. It improves readability when skipping many conditions. - Combine with
IsError()to skip cells containing #N/A, #VALUE!, etc.:If IsError(rng.Cells(i).Value) Then Continue For. - Use
Continue ForwithIsEmpty()to bypass blank cells:If IsEmpty(rng.Cells(i)) Then Continue For. - For performance, avoid using
Continue Forin very large loops (e.g., 100,000+ rows). Consider filtering data with AutoFilter or array processing instead. - Always test your loop logic with a small dataset first (e.g., 5–10 rows) before scaling up.
- Comment your
Continue Forstatements clearly: 'skip empty or zero values' helps future maintainers.
Troubleshooting
- Error: "Compile error: Invalid outside procedure" — You placed
Continue Foroutside a subroutine or function. Ensure it’s inside aSuborFunctionand within aFor...Nextloop. - Loop doesn’t skip any values — Check your condition. Use
Debug.Print currentValuebefore theContinue Forto verify the actual values being evaluated. - Loop runs infinitely — Never modify the loop variable (e.g.,
i = i + 1) inside the loop. LetNext ihandle it. If you changei, the loop counter becomes corrupted. - Code runs but no visual result — Ensure your worksheet isn’t protected. Also check that the cells aren’t formatted with conditional formatting that overrides your color changes.
- Macro disabled warning — Enable macros via File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros (for testing only) or enable for this session.
- Runtime error 9: Subscript out of range — Your range doesn’t exist. Double-check the range reference (e.g.,
"A1:A10") matches actual sheet data. UseRange("A1").CurrentRegionfor dynamic ranges.
Frequently Asked Questions
Can I use Continue For in a Do While or Do Until loop?
No.Continue For only works within For...Next loops. For Do loops, use an If statement with Exit Do to break out, or restructure with a For loop.
Does Continue For skip the Next statement?
Yes. WhenContinue For executes, it immediately jumps to the next iteration, bypassing any code after it in the current loop cycle, including the Next line.
Can I use Continue For with multiple nested loops?
Yes, but it only affects the innermostFor loop. To skip an outer loop, you must use flags or restructuring with boolean variables.
Is Continue For faster than using If-Else blocks?
It’s often cleaner and more readable, but performance is nearly identical. The real benefit is code clarity and reduced nesting, not speed.What if my loop variable is a string or object?
Continue For works regardless of the variable type. It only cares about the loop structure. For example: For Each cell In Range("A1:A10") can also use Continue For.