How to Use For Loop Skip to Next Iterations In Excel VBA

Method 1 – ‘Skip to Next’ Iteration in the ‘For-Next Loop’ with Step Statement

 

Below is a dataset of different St. IDs and their marks in different subjects. We will show you how to highlight alternate rows using a simple For Loop with the Step statement.

2- Dataset to skip to next iteration with For loop in Excel VBA.

Steps:

  • Select a specific date range. We have selected B5:E16.
  • Insert this Code in a new Module.

3.VBA Code for skipping to next with For Next loop.

Sub Skip_to_next_with_step()
For i = 2 To Selection.Rows.Count Step 2
Selection.Rows(i).Interior.ColorIndex = 35
Next i
End Sub
  • Click on the Run button.

VBA Code Breakdown

For i = 2 To Selection.Rows.Count Step 2
  • This line initiates a For loop. Selection.Rows.Count gives the number of Rows in the selected range. The Step 2 section sets the value of i as 2,4,6…..etc.
Selection.Rows(i).Interior.ColorIndex = 35
Next i
  • This section of the code sets the interior color of the rows(i) and moves to the next value of i. So, with this code, you can highlight alternate rows in a selected range.

Running the code, your data table will look like this, and all the alternative rows will be highlighted.

4.Showing alternate colored rows.


Method 2 – Skipping Through The GoTo Statement

In our dataset, we will use the GoTo statement to skip iteration. Let’s assume the pass mark is 40. In this procedure, we will highlight the mark cells based on the pass mark.

5- Dataset to be used for GoTo statement method.

Steps:

  • Select the data range consisting of only marks. For our dataset, we have selected the cell range C5:E16.
  • Open a new module in your workbook and Insert the following code:

6- VBA Code for skip to next with GoTo statement.

Sub skip_Through_GoTo()
    For Each cell In Selection
        If cell.Value >= 40 Then
            cell.Interior.Color = RGB(191, 249, 193)
        Else
        GoTo Failed_Criteria
        End If
Failed_Criteria:
     If cell.Value < 40 Then
            cell.Interior.Color = RGB(255, 168, 168)
     End If
        Next cell         
End Sub
  • Click on the Run button.

VBA Code Breakdown

For Each cell In Selection
        If cell.Value >= 40 Then
            cell.Interior.Color = RGB(191, 249, 193)

This portion of the code checks every cell value in the selected range whether the value is equal to or greater than 40. If the cell value is equal to or more than 40, the interior color will be highlighted as RGB(191,249,193).

 Else
        GoTo Failed_Criteria
        End If

In case the previous condition doesn’t meet, then this GoTo statement will take to the Failed_Criteria label which is stated in the later part of the code.

Failed_Criteria:
 If cell.Value < 40 Then
            cell.Interior.Color = RGB(255, 168, 168)
     End If
        Next cell

In this Failed_Criteria section, we have inserted another condition. If the cell value is less than 40, the interior color will be highlighted according to RGB(255, 168, 168).

Your data table will look like the below image, where the cells with pass marks will be highlighted in green and the cells with fail marks will be highlighted in red.

7- Showing different interior colors for different criteria.


How to Continue a Do-While Loop in Excel VBA

We have taken this dataset of St. IDs and their marks for different subjects.

8- Dataset to be used for the Do While loop.

With the Do-While Loop, we will find out the cells with failed and passed Criteria.

Steps:

  •  Enter this VBA code in a new Module and click the Run button.

9- VBA Code for a Do While loop.

Sub skip_Through_GoTo_Do_while()
Dim rw, cl As Integer
rw = 5
Do While rw < 17
cl = 3
    Do While cl < 6
        If Cells(rw, cl).Value >= 40 Then
            Cells(rw, cl).Interior.Color = RGB(191, 249, 193)
        Else
            GoTo Failed_Criteria
        End If
Failed_Criteria:
 If Cells(rw, cl).Value < 40 Then
            Cells(rw, cl).Interior.Color = RGB(255, 168, 168)
     End If
     cl = cl + 1
     Loop
rw = rw + 1
Loop
End Sub

VBA Code Breakdown

Sub skip_Through_GoTo_Do_while()
Dim rw, cl As Integer
rw = 5
Do While rw < 17
cl = 3
    Do While cl < 6

The code takes rw and cl as integers. Also, it sets the value of rw as 5 and initiates a Do While loop. Then, it takes the value of cl as 3 and initiates another Do-While Loop.

If Cells(rw, cl).Value >= 40 Then
        Cells(rw, cl).Interior.Color = RGB(191, 249, 193)

This section contains an If loop with the condition of passed criteria. Upon fulfilling the condition, the interior color of the cell will be changed.

  Else
            GoTo Failed_Criteria
   End If
If the previous condition does not satisfy, then the code will skip the loop and will move on to the Failed_Criteria label.
Failed_Criteria:
 If Cells(rw, cl).Value < 40 Then
            Cells(rw, cl).Interior.Color = RGB(255, 168, 168)
     End If
     cl = cl + 1
     Loop
rw = rw + 1
Loop

This portion will begin a new IF loop, checking whether the cell value is less than 40. If so, the cell color will be changed.

10- Result with different colors for different criteria.

Read More: How to Continue Excel VBA For Loop


How to Continue a Do-Until Loop in Excel VBA

We will use the same dataset as shown before.

11- Dataset to be used for a Do Until loop.

Steps:

  • Enter the below code in a new module:

12- VBA Code to be used for a Do While loop

Sub skip_Through_GoTo_Do_until()
Dim rw, cl As Integer
rw = 5
Do Until rw = 17
cl = 3
    Do Until cl = 6
        If Cells(rw, cl).Value >= 40 Then
            Cells(rw, cl).Interior.Color = RGB(191, 249, 193)
        Else
            GoTo Failed_Criteria
        End If
Failed_Criteria:
     If Cells(rw, cl).Value < 40 Then
            Cells(rw, cl).Interior.Color = RGB(255, 168, 168)
     End If
     cl = cl + 1
    Loop
rw = rw + 1
Loop
End Sub
  • Run the Module.

VBA Code Breakdown

Sub skip_Through_GoTo_Do_until()
Dim rw, cl As Integer
rw = 5
Do Until rw = 17
cl = 3
    Do Until cl = 6

The code takes rw and cl as integers. Also, it sets the value of rw as 5 and initiates a Do Until loop. Then, it takes the value of cl as 3 and initiates another Do-Until Loop.

 If Cells(rw, cl).Value >= 40 Then
        Cells(rw, cl).Interior.Color = RGB(191, 249, 193)

This section contains an If loop with the condition of passed criteria. Upon fulfilling the condition, the interior color of the cell will be changed.

  Else
            GoTo Failed_Criteria
       End If

If the previous condition does not satisfy, then the code will skip the loop and will move on to the Failed_Criteria label.

Failed_Criteria:
   If Cells(rw, cl).Value < 40 Then
            Cells(rw, cl).Interior.Color = RGB(255, 168, 168)
     End If
     cl = cl + 1
     Loop
rw = rw + 1
Loop

This portion will begin a new IF loop where it will check whether the cell value is less than 40. If so, the cell color will be changed.

  • Enter the following code in a new module.
  • Click on the Run button. We can see all the cells are highlighted accordingly.

13- Indicating different criteria with different colors.


How to Use Excel VBA Loops to Iterate Through Arrays and Ranges

 

1. VBA Loop to Iterate Through Ranges

In this procedure, we will add a Remarks based on Total marks, so we added a new column named Remarks.

14- Dataset to iterate through Ranges.

Steps:

  • Select the data range. We selected the range  F5:F16.
  • Enter the following code in a new Module and Click on the Run button.

15- VBA Code to iterate through Ranges.

Sub Loop_Rng()
For i = 1 To Selection.Rows.Count
If Range("F" & i + 4).Value > 200 Then
   Range("G" & i + 4).Value = "Good"
   Range("G" & i + 4).Interior.Color = RGB(20, 200, 120)
ElseIf Range("F" & i + 4).Value >= 150 _
And Range("F" & i + 4).Value < 200 Then
   Range("G" & i + 4).Value = "Average"
   Range("G" & i + 4).Interior.Color = RGB(20, 150, 150)
Else
  Range("G" & i + 4).Value = "Poor"
  Range("G" & i + 4).Interior.Color = RGB(170, 100, 100)
  End If
Next i
End Sub

VBA Code Breakdown

Dim i As Integer
For i = 1 To Selection.Rows.Count

First, we have taken i as Integer. And this code runs from the i value 1 to the number of selected rows.

If Range("F" & i + 4).Value > 200 Then
   Range("G" & i + 4).Value = "Good"
   Range("G" & i + 4).Interior.Color = RGB(20, 170, 120)

This section initiates an IF Loop where it checks whether the cell value in Total Column is equal to or greater than 200. If the condition is satisfied, then the adjacent cell in the next column takes “Good” as input, and also the interior color is changed.

ElseIf Range("F" & i + 4).Value >= 150 _
And Range("F" & i + 4).Value < 200 Then
   Range("G" & i + 4).Value = "Average"
   Range("G" & i + 4).Interior.Color = RGB(20, 160, 170)

This section has another condition. If the previous condition is not satisfied then the code will check this condition whether the Total mark is between 150 and 200. If so, the cell in the next column will take the input “Average” and the interior color will also be changed.

Else
  Range("G" & i + 4).Value = "Poor"
  Range("G" & i + 4).Interior.Color = RGB(170, 100, 100)
  End If
Next i

If the previous two conditions are not satisfied, then the adjacent cell of the next column will take “Poor” as input, and the interior color will also be changed.

  • After running the code, we get the Remarks Column filled with proper Remarks.

16- Showing different groups with different colors.


2. VBA Loop to Iterate Through Arrays

We have the same dataset as the previous procedures. The dataset has the Total marks of each student. Now, with the help of the VBA Array, we will add Remarks based on different criteria.

17- Dataset to iterate through Arrays

Steps:

  • Enter the code in a new module.
  • Click on the Run button.

18- VBA Code to iterate through Arrays.

Sub array_summation()
    Dim marksArray(1 To 12) As Variant
    Dim i As Integer
    For i = 1 To 12
    marksArray(i) = Range("F" & i + 4).Value
    Next i
    For i = 1 To 12
    If marksArray(i) >= 200 Then
    Range("G" & i + 4).Value = "Good"
    Range("G" & i + 4).Interior.Color = RGB(20, 200, 120)
    ElseIf marksArray(i) >= 150 And _
            marksArray(i) < 200 Then
       Range("G" & i + 4).Value = "Average"
       Range("G" & i + 4).Interior.Color = RGB(20, 150, 150)
    Else
    Range("G" & i + 4).Value = "Poor"
    Range("G" & i + 4).Interior.Color = RGB(170, 100, 100)
    End If
    Next i
End Sub

VBA Code Breakdown

Dim marksArray(1 To 12) As Variant
    Dim i As Integer
    For i = 1 To 12
    marksArray(i) = Range("F" & i + 4).Value
    Next i

First, we have taken marksArray(1 to 12) as an Array. Then we used a For Loop for assigning data range in that Array. The input Range contains Total marks.

 For i = 1 To 12
    If marksArray(i) >= 200 Then
    Range("G" & i + 4).Value = "Good"
    Range("G" & i + 4).Interior.Color = RGB(20, 200, 120)

Again we have initiated For Loop. Then within the If statement we included the first condition. If the value is equal to or greater than 200, the next cell in the adjacent column will take “Good” as input.

ElseIf marksArray(i) >= 150 And _
            marksArray(i) < 200 Then
       Range("G" & i + 4).Value = "Average"
       Range("G" & i + 4).Interior.Color = RGB(20, 150, 150)

If the previous condition doesn’t satisfy the program will execute this portion. If the Total mark is greater than or equal to 150 and also less than 200, the next cell in the adjacent Column will take “Average” as input.

Else
    Range("G" & i + 4).Value = "Poor"
    Range("G" & i + 4).Interior.Color = RGB(170, 100, 100)
    End If
    Next i

If the previous two conditions aren’t satisfied, then the next cell in the adjacent Column will take “Poor” as input.

You will get the Remarks in the following image.

19- Showing different groups with different colors.

Read More: Excel VBA For Loop with Array


Download the Practice Workbook

You can download and practice this workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Sourav Kundu
Sourav Kundu

Sourav Kundu, BSc, Naval Architecture & Marine Engineering, Bangladesh University of Engineering and Technology, is a dedicated technical content creator of the ExcelDemy project. He has a keen interest in Excel and he leverages his problem-solving skills to provide solutions on user interface with Excel. In his position as an Excel & VBA Content Developer at ExcelDemy, Sourav Kundu not only adeptly addresses challenging issues but also demonstrates enthusiasm and expertise in navigating complex situations. Apart from creating... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo