This how-to quick snapshot video shows you examples of using For Next statement for basic counting in Excel VBA. Remember, this snapshot is not a full tutorial.
How to show basic counting using For Next statement
We use Debug.Print to show results of different ways to count numbers from zero to ten. Debug.Print is a great way to test or check your code.
Video
In addition to using the For Next statement, this video also shows you some direct Debug.Print techniques in the Immediate window.
Code samples
Here are some code samples of counting from zero to ten, one to ten, and vice versa. Results are right below code.
Sample 1 – zero to ten
Public Sub zeroToTen()
For i = 0 To 10
Debug.Print i
Next i
End Sub
Results…
Sample 2 – one to ten
Public Sub oneToTen()
For i = 1 To 10
Debug.Print i
Next i
End Sub
Results…
Sample 3 – ten to one
Public Sub tenToOne()
For i = 10 To 1 Step -1
Debug.Print i
Next i
End Sub
Results…
Sample 4 – ten to zero
Public Sub tenToZero()
For i = 10 To 0 Step -2
Debug.Print i
Next i
End Sub
Results…