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

  1. Open Excel and press Alt + F11 to open the Visual Basic for Applications editor.
  2. In the Project Explorer (left pane), right-click on your workbook name (e.g., VBAProject (YourWorkbook.xlsx)), then select Insert > Module. A blank code window will appear.
  3. Declare your subroutine by typing: Sub SkipIterations() and press Enter to auto-generate the closing End Sub.
  4. Inside the subroutine, define a loop variable and set a range to loop through. For example: Dim i As Long and Dim rng As Range.
  5. Set the range you want to iterate. For instance: Set rng = Range("A1:A10"). Adjust the range to match your data.
  6. Start the For loop: For i = 1 To rng.Cells.Count.
  7. Inside the loop, use rng.Cells(i).Value to access the value of the current cell. For example: Dim currentValue As Variant followed by currentValue = rng.Cells(i).Value.
  8. 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.
  9. After the Continue For statement, 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.
  10. Close the loop with Next i.
  11. Optionally, add a message box to confirm completion: MsgBox "Loop completed. Non-zero cells highlighted."
  12. Save your workbook as a macro-enabled file: File > Save As > Excel Macro-Enabled Workbook (*.xlsm).
  13. Return to Excel by pressing Alt + Q, then press Alt + F8, select SkipIterations, and click Run.

Common Mistakes

  • Using Continue For outside a For loop—this causes a compile error. It only works inside a For...Next block.
  • Typing Continue instead of Continue For. The full keyword is required; partial syntax is invalid.
  • Placing Continue For after other logic that modifies the loop variable (e.g., changing i manually). This breaks the loop’s integrity.
  • Using Continue For in 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 For to avoid nested If...Then...Else blocks. 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 For with IsEmpty() to bypass blank cells: If IsEmpty(rng.Cells(i)) Then Continue For.
  • For performance, avoid using Continue For in 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 For statements clearly: 'skip empty or zero values' helps future maintainers.

Troubleshooting

  • Error: "Compile error: Invalid outside procedure" — You placed Continue For outside a subroutine or function. Ensure it’s inside a Sub or Function and within a For...Next loop.
  • Loop doesn’t skip any values — Check your condition. Use Debug.Print currentValue before the Continue For to verify the actual values being evaluated.
  • Loop runs infinitely — Never modify the loop variable (e.g., i = i + 1) inside the loop. Let Next i handle it. If you change i, 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. Use Range("A1").CurrentRegion for 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. When Continue 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 innermost For 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.

Will Continue For work in Excel Online or mobile?

No. VBA is not supported in Excel Online, Excel for iPad, or Excel for Android. This feature only works in desktop versions of Excel for Windows or Mac with full VBA support.