Basic counting Excel VBA using For Next – How-to

Basic counting Excel VBA using For Next – How-to

Basic counting Excel VBA using For Next - How-to - Excel Effects

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…

Results - zero to ten - Excel Effects

Sample 2 – one to ten

Public Sub oneToTen()
    For i = 1 To 10
        Debug.Print i
    Next i
End Sub

Results…

Basic counting - Results one to ten - Excel Effects

Sample 3 – ten to one

Public Sub tenToOne()
    For i = 10 To 1 Step -1
        Debug.Print i
    Next i
End Sub

Results…

Results ten to one - Excel Effects

Sample 4 – ten to zero

Public Sub tenToZero()
    For i = 10 To 0 Step -2
        Debug.Print i
    Next i
End Sub

Results…

Basic counting - Results ten to zero - Excel Effects

Related to basic counting

 

Affiliate stuff – Move up and excel with Excel