Home » Tutorials » VBA Tutorials » VBA – Loops

VBA – Loops

VBA - Loops Shout4Education
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. Following is the general form of a loop statement in VBA.
VBA - Loops Shout4Education
VBA provides the following types of loops to handle looping requirements. Click the following links to check their detail.
Sr.No. Loop Type & Description
1 for loop

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

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

This tests the condition before executing the loop body.
4 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.
5 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.

VBA – For Loops

for loop is a repetition control structure that allows a developer to efficiently write a loop that needs to be executed a specific number of times.

Syntax

Following is the syntax of a for loop in VBA.
For counter = start To end [Step stepcount]
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
   ....
   [statement n]
Next

Flow Diagram

Flow Diagram of VBA - For Loops Shout4Education
Following is the flow of control in a For Loop −
  • The For step is executed first. This step allows you to initialize any loop control variables and increment the step counter variable.
  • Secondly, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement, just after the For Loop.
  • After the body of the For loop executes, the flow of control jumps to the next statement. This statement allows you to update any loop control variables. It is updated based on the step counter value.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the For Loop terminates.

Example

Add a button and add the following function.
Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 10

 

For i =  To a Step 2
      MsgBox "The value is i is : " & i
   Next
End Sub
When the above code is compiled and executed, it produces the following result.
The value is i is : 0

The value is i is : 2

Related:  VBA - Excel Objects

The value is i is : 4

The value is i is : 6

The value is i is : 8

The value is i is : 10

VBA – For Each Loop

For Each loop is used to execute a statement or a group of statements for each element in an array or collection.
A For Each loop is similar to For Loop; however, the loop is executed for each element in an array or group. Hence, the step counter won’t exist in this type of loop. It is mostly used with arrays or used in the context of the File system objects in order to operate recursively.

Syntax

Following is the syntax of a For Each loop in VBA.
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
When the above code is executed, it prints all the fruit names with one item in each line.
apple
orange
cherries

VBA – While Wend Loop

In a While…Wend loop, if the condition is True, all the statements are executed until the Wend keyword is encountered.
If the condition is false, the loop is exited and the control jumps to the very next statement after the Wend keyword.

Syntax

Following is the syntax of a While..Wend loop in VBA.
While condition(s)
   [statements 1]
   [statements 2]
   ...
   [statements n]
Wend

Flow Diagram

Flow Diagram of VBA - While Wend Loops Shout 4 Education

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   
When the above code is executed, it prints the following in a message box.
The Current Value of the Counter is : 11

The Current Value of the Counter is : 12

The Current Value of the Counter is : 13

The Current Value of the Counter is : 14

The Current Value of the Counter is : 15

VBA – Do-While Loop

Do…While loop is used when we want to repeat a set of statements as long as the condition is true. The condition may be checked at the beginning of the loop or at the end of the loop.

Syntax

Following is the syntax of a Do…While loop in VBA.
Do While condition
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop

Flow Diagram

Flow Diagram VBA - Do-While Loops Shout For Education

Example

The following example uses Do…while loop to check the condition at the beginning of the loop. The statements inside the loop are executed, only if the condition becomes True.
Private Sub Constant_demo_Click()
   Do While i < 5
      i = i + 1
      msgbox "The value of i is : " & i
   Loop
End Sub
When the above code is executed, it prints the following output in a message box.
The value of i is : 1

The value of i is : 2

Related:  VBA - Strings

The value of i is : 3

The value of i is : 4

The value of i is : 5

Alternate Syntax

There is also an alternate Syntax for Do…while loop which checks the condition at the end of the loop. The major difference between these two syntaxes is explained in the following example.
Do 
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop While condition

Example

The following example uses Do…while loop to check the condition at the end of the loop. The statements inside the loop are executed at least once, even if the condition is False.
Private Sub Constant_demo_Click() 
   i = 10
   Do
      i = i + 1
      MsgBox "The value of i is : " & i
   Loop While i < 3 'Condition is false.Hence loop is executed once.
End Sub
When the above code is executed, it prints the following output in a message box.
The value of i is : 11

VBA – Do-Until Loop

Do…Until loop is used when we want to repeat a set of statements as long as the condition is false. The condition may be checked at the beginning of the loop or at the end of the loop.

Syntax

Following is the syntax of a Do..Until loop in VBA.
Do Until condition
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop

Flow Diagram

Flow Diagram of VBA - Do-Until Loops Shout4Education

Example

The following example uses Do…Until loop to check the condition at the beginning of the loop. The statements inside the loop are executed only if the condition is false. It exits out of the loop when the condition becomes true.
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
When the above code is executed, it prints the following output in a message box.
The value of i is : 11

The value of i is : 12

The value of i is : 13

The value of i is : 14

The value of i is : 15

The value of i is : 16

Alternate Syntax

There is also an alternate syntax for Do…Until loop which checks the condition at the end of the loop. The major difference between these two syntaxes is explained with the following example.
Do 
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop Until condition

Flow Diagram

Flow Diagram of VBA - Do-Until Loops Shout4Education

Example

The following example uses Do…Until loop to check the condition at the end of the loop. The statements inside the loop are executed at least once, even if the condition is True.
Private Sub Constant_demo_Click()  
   i = 10
   Do 
      i = i + 1
      msgbox "The value of i is : " & i
   Loop Until i<15 'Condition is True.Hence loop is executed once.
End Sub
When the above code is executed, it prints the following output in a message box.
The value of i is : 11

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. Click the following links to check their detail.
S.No. Control Statement & Description
1 Exit For statement

Terminates the For loop statement and transfers the execution to the statement immediately following the loop
2 Exit Do statement

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

VBA – Exit For

Exit For statement is used when we want to exit the For Loop based on certain criteria. When Exit For is executed, the control jumps to the next statement immediately after the For Loop.

Syntax

Following is the syntax for Exit For Statement in VBA.
 Exit For

Flow Diagram

Flow Diagram of VBA - Exit For Shout For Education

Example

The following example uses Exit For. If the value of the Counter reaches 4, the For Loop is exited and the control jumps to the next statement immediately after the For Loop.
Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 10

 

For i =  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
When the above code is executed, it prints the following output in a message Box.
The value is i is : 0

The value is i is : 2

Related:  VBA - Programming Charts

The value is i is : 4

The value is i is : 40

VBA – Exit Do

An Exit Do Statement is used when we want to exit the Do Loops based on certain criteria. It can be used within both Do…While and Do…Until Loops.
When Exit Do is executed, the control jumps to the next statement immediately after the Do Loop.

Syntax

Following is the syntax for Exit Do Statement in VBA.
 Exit Do

Example

The following example uses Exit Do. If the value of the Counter reaches 10, the Do Loop is exited and the control jumps to the next statement immediately after the For Loop.
Private Sub Constant_demo_Click()
   i = 
   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
When the above code is executed, it prints the following output in a message box.
The Value of i is : 0

The Value of i is : 2

The Value of i is : 4

The Value of i is : 6

The Value of i is : 8

The Value of i is : 10

Leave a Comment