For loop it Executes a sequence of statements multiple times and reduces the code that manages the loop variable.
Syntax
Following is the syntax of a for loop in VBA
For counter = start To end [Step stepcount] ' step counter is used when we want to add counter.
[Exit For] ' it is used when we want to exit the loop when a certain condition is met
Next ' increase the counter by one for the next loop
[Exit For] ' it is used when we want to exit the loop when a certain condition is met
Next ' increase the counter by one for the next loop
Following is the flow of control in a For Loop
· The For step is executed
first. This step allows us to initialize any loop 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, then
it terminates the loop and exit out of the loop and execute the remaining codes
·
Then counter is
incremented and the condition is now evaluated again. If it is true, the loop
executes and the process repeats itself After the condition becomes false, the
For Loop terminates
Sub demo_Forloop()
Dim a As Integer
a = 15
For i = 0 To a Step 3 ' step is 3 so loop will repeat every third time
MsgBox "The value is i is : "
& i
Next
End Sub
|
OutPut |
The value is i is : 0
The value is i is : 3
The value is i is : 6
The value is i is : 9
The value is i is : 12
The value is i is : 15
|

0 Comments