Skip to Content
Course content

2.4.1 Loops in VBA: For...Next

The For...Next loop in VBA is used to repeat a block of code a specified number of times. It is one of the most commonly used looping structures in VBA, allowing you to iterate over a series of values, such as a range of numbers or elements in an array, and perform repetitive tasks.

1. Syntax of the For...Next Loop

The basic syntax of the For...Next loop is:

For counter = start_value To end_value [Step step_value]
    ' Code to execute in each iteration
Next counter
  • counter: A variable that controls the loop's iteration.
  • start_value: The initial value of the counter.
  • end_value: The final value the counter will reach.
  • step_value (optional): The amount by which the counter will be incremented or decremented after each iteration. If not specified, the default is 1.

2. Basic Example of a For...Next Loop

Here's a simple example of using the For...Next loop to print numbers from 1 to 5:

Dim i As Integer

For i = 1 To 5
    MsgBox "Iteration " & i
Next i

In this example:

  • The loop starts with i = 1 and repeats until i reaches 5.
  • The MsgBox displays the current value of i in each iteration.

3. Using the Step Keyword

The Step keyword allows you to specify the increment (or decrement) value for the loop counter. By default, the counter increments by 1, but you can change this behavior with the Step keyword.

Example with Step:

For i = 1 To 10 Step 2
    MsgBox "Iteration " & i
Next i

In this example, the loop starts at 1 and increments by 2 in each iteration, printing the values 1, 3, 5, 7, and 9.

Example with Negative Step (Decrementing):

For i = 10 To 1 Step -1
    MsgBox "Iteration " & i
Next i

In this case, the loop starts at 10 and decrements by 1 in each iteration, printing the values 10, 9, 8, ..., down to 1.

4. Using the For...Next Loop with Arrays

The For...Next loop is often used to iterate over the elements of an array. Here’s an example of iterating through an array of numbers:

Dim numbers(5) As Integer
Dim i As Integer

' Assign values to the array
numbers(0) = 10
numbers(1) = 20
numbers(2) = 30
numbers(3) = 40
numbers(4) = 50

For i = 0 To 4
    MsgBox "Array value at index " & i & " is " & numbers(i)
Next i

This example uses a For...Next loop to iterate through each element of the numbers array, displaying its value in a message box.

5. Nested For...Next Loops

You can nest one For...Next loop inside another to perform repetitive tasks for multiple ranges or dimensions, such as iterating over rows and columns in a table or range.

Example of Nested For...Next Loop:

Dim i As Integer, j As Integer

For i = 1 To 3
    For j = 1 To 2
        MsgBox "i = " & i & ", j = " & j
    Next j
Next i

In this example, the outer loop iterates from 1 to 3, and the inner loop iterates from 1 to 2. The combination of both loops results in a total of 6 message boxes showing all pairs of i and j.

6. Exiting the For...Next Loop Early (Exit For)

You can use the Exit For statement to break out of a loop before it reaches the specified end_value. This is useful when a certain condition is met, and you no longer need to continue the loop.

Example of Exit For:

Dim i As Integer

For i = 1 To 10
    If i = 5 Then
        MsgBox "Condition met, exiting the loop."
        Exit For
    End If
    MsgBox "Iteration " & i
Next i

In this example, when i reaches 5, the loop exits early, and the message box displays "Condition met, exiting the loop."

7. Conclusion

The For...Next loop is a powerful and flexible tool for performing repetitive tasks in VBA. Whether you're iterating over a set range of values, working with arrays, or using step increments, this loop structure allows you to automate repetitive processes efficiently. By combining it with other control structures, you can create complex logic to meet the needs of your program.

Commenting is not enabled on this course.