VBA - Loops

A loop statement allows us to execute a statement or group of statements multiple times.VBA provides the following types of loops to handle looping requirements.

VBA For loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

Syntax:-

For counter = start To end [Step stepcount]
[statement 1]
[statement 2]
....
[statement n]
[Exit For]
[statement 11]
[statement 22]
....
[statement n]
Next

Example:-

Private Sub Constant_demo_Click()
Dim a As Integer
a = 10
For i = 0 To a Step 2
MsgBox "The value is i is : " & i
Next
End Sub

VBA For ..each loop

This is executed if there is at least one element in the group and reiterated for each element in a group.

Syntax:-

For Each element In Group
[statement 1]
[statement 2]
....
[statement n]
[Exit For]
[statement 11]
[statement 22]
Next

Example:-

Private Sub Constant_demo_Click()
'fruits is an array
fruits = Array("apple", "orange", "cherries")
Dim fruitnames As Variant
'iterating using For each loop.
For Each Item In fruits
fruitnames = fruitnames & Item & Chr(10)
Next
MsgBox fruitnames
End Sub

VBA while..wend loop

This tests the condition before executing the loop body.

Syntax:-

While condition(s)
[statements 1]
[statements 2]
...
[statements n]
Wend

Example:-

Private Sub Constant_demo_Click()
Dim Counter : Counter = 10
While Counter < 15 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
msgbox "The Current Value of the Counter is : " & Counter
Wend ' While loop exits if Counter Value becomes 15.
End Sub

VBA do..while loops

The do..While statements will be executed as long as the condition is True.(i.e.,) The Loop should be repeated till the condition is False.

Syntax:-

Do While condition
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop

Example:-

Private Sub Constant_demo_Click()
Do While i < 5
i = i + 1
msgbox "The value of i is : " & i
Loop
End Sub

VBA do..until loops

The do..Until statements will be executed as long as the condition is False.(i.e.,) The Loop should be repeated till the condition is True.

Syntax:-

Do Until condition
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop

Example:-

Private Sub Constant_demo_Click()
i = 10
Do Until i>15 'Condition is False.Hence loop will be executed
i = i + 1
msgbox ("The value of i is : " & i)
Loop
End Sub

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all the remaining statements in the loop are NOT executed.VBA supports the following control statements.

Exit For statement

Terminates the For loop statement and transfers the execution to the statement immediately following the loop

Syntax:-

Exit For

Example:-

Private Sub Constant_demo_Click()
Dim a As Integer
a = 10
For i = 0 To a Step 2 'i is the counter variable and it is incremented by 2
MsgBox ("The value is i is : " & i)
If i = 4 Then
i = i * 10 'This is executed only if i=4
MsgBox ("The value is i is : " & i)
Exit For 'Exited when i=4
End If
Next
End Sub

Exit Do statement

Terminates the Do While statement and transfers the execution to the statement immediately following the loop

Syntax:-

Exit Do

Example:-

Private Sub Constant_demo_Click()
i = 0
Do While i <= 100
If i > 10 Then
Exit Do ' Loop Exits if i>10
End If
MsgBox ("The Value of i is : " & i)
i = i + 2
Loop
End Sub