Introduction
When using a For loop in VBA, you often encounter situations where you want to “skip the rest of the processing for the current loop and move immediately to the next iteration” only when a specific condition is met.
Many other programming languages provide a specific command for this, such as Continue For. However, VBA does not have a direct equivalent.
Fortunately, you can easily achieve the same behavior by structuring your If statements correctly. This article explains the most common and recommended way to skip processing in a loop and proceed to the next iteration.
VBA Sample Code to Skip Processing
This macro checks a product list row by row. If the “Inventory Status” (Column C) is “Out of Stock,” it skips the price calculation for that row and moves to the next product.
' Skip subsequent processing in the loop based on a specific condition
Sub SkipLoopIteration()
Dim i As Long
Const DISCOUNT_RATE As Double = 0.9 ' 10% Discount
' Loop from row 2 to 10
For i = 2 To 10
'--- Check Inventory Status in Column C ---
' If the value is NOT "Out of Stock", execute the processing inside the If block
If Cells(i, "C").Value <> "Out of Stock" Then
'--- Main processing (Do not skip) ---
' Multiply price in Column D by discount rate and calculate discounted price in Column E
Cells(i, "E").Value = Cells(i, "D").Value * DISCOUNT_RATE
Debug.Print "Processed row " & i
End If
' If "Out of Stock", the If block is not executed, and the code goes directly to Next i
Next i
MsgBox "Processing complete."
End Sub
Explanation of the Code
The Key Structure: If … Then … End If
The key to skipping processing lies in this If block structure:
If Cells(i, "C").Value <> "Out of Stock" Then
' ... Process ...
End If
- Condition: The code checks if the value in Column C is NOT “Out of Stock”.
- True: If the condition is true (e.g., “In Stock” or blank), the main processing (price calculation) written between
IfandEnd Ifis executed. - False: If the condition is false (i.e., it IS “Out of Stock”), all processing between
IfandEnd Ifis skipped. The program jumps directly to the line afterEnd If, which isNext i. This effectively moves the loop to the next iteration.
Why Avoid “GoTo”?
It is technically possible to use the GoTo statement to jump to a label at the end of the loop to achieve a similar result. However, this is generally not recommended.
Overusing GoTo disrupts the natural top-to-bottom flow of the program, leading to “spaghetti code” that is difficult to read and maintain. Unless there is a very specific reason, wrapping your processing in an If block is the safer, cleaner, and recommended approach.
Summary
In this article, we explained how to skip processing in a specific iteration of a VBA For loop.
- VBA does not have a specific command like
Continue For. - The simplest and most recommended method is to wrap the entire main process in an
If Condition Then ... End Ifblock. - By using this technique, you can write clean code that flexibly processes only the data rows that meet specific criteria.
