MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Flow control structures

For loop


The For loop is used to repeat the enclosed section of code a given number of times. The following simple example illustrates the basic syntax:

Dim i as Integer 'Declaration of i
For i = 1 to 10 'Declare how many times the loop shall be executed
 Debug.Print i 'The piece of code which is repeated
Next i 'The end of the loop

The code above declares an Integer i. The For loop assigns every value between 1 and 10 to i and then executes Debug.Print i - i.e. the code prints the numbers 1 through 10 to the immediate window. Note that the loop variable is incremented by the Next statement, that is after the enclosed code executes as opposed to before it executes.

By default, the counter will be incremented by 1 each time the loop executes. However, a Step can be specified to change the amount of the increment as either a literal or the return value of a function. If the starting value, ending value, or Step value is a floating point number, it will be rounded to the nearest integer value. Step can be either a positive or negative value.

Dim i As Integer
For i = 1 To 10 Step 2
 Debug.Print i 'Prints 1, 3, 5, 7, and 9
Next

In general a For loop would be used in situations where it is known before the loop starts how many times to execute the enclosed code (otherwise a Do or While loop may be more appropriate). This is because the exit condition is fixed after the first entry into loop, as this code demonstrates:

Private Iterations As Long 'Module scope
Public Sub Example()
 Dim i As Long
 Iterations = 10
 For i = 1 To Iterations
 Debug.Print Iterations 'Prints 10 through 1, descending.
 Iterations = Iterations - 1
 Next
End Sub

A For loop can be exited early with the Exit For statement:

Dim i As Integer
For i = 1 To 10
 If i > 5 Then
 Exit For
 End If
 Debug.Print i 'Prints 1, 2, 3, 4, 5 before loop exits early.
Next


Conclusion

In this page (written and validated by ) you learned about VBA Flow control structures . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Passing Arguments ByRef or ByVal.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.