The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
A For...Next loop repeats a block of Visual Basic code while a counter moves from a starting value toward an ending value. The starting value, ending value, and optional Step determine the counter’s sequence; Next advances it. The loop stops after the counter passes the bound, unless the code exits earlier.
What a For…Next loop does
A For...Next loop is a counter-controlled loop: it runs the statements between For and Next for each value in a numeric progression. Microsoft describes it as repeating statements while the loop counter approaches its final value. The construct appears in both Visual Basic .NET and VBA, though their syntax differs slightly.
For example, a loop from 1 to 5 with the default step processes the counter values 1, 2, 3, 4, and 5. The ending value is included when the progression reaches it.
Syntax in Visual Basic .NET and VBA
In Visual Basic .NET, the general form is:
For counter [ As datatype ] = start To end [ Step step ]
statements
Next [ counter ]
The datatype declaration is optional. In VBA, omit the optional datatype clause:
#1 Best Overall
For counter = start To end [ Step step ]
statements
Next [ counter ]
The counter name after Next is optional. Including it can make code easier to follow, especially when loops are nested. See Microsoft’s Visual Basic .NET For…Next reference and VBA For…Next reference for the language-specific rules.
How start, end, and Step determine the iterations
start sets the counter’s initial value, and end sets its limit. If you omit Step, the counter advances by 1. A negative step makes it count down. Choose a step whose direction moves the counter toward the end value.
Count upward
This Visual Basic .NET example writes the values 1 through 5:
For index As Integer = 1 To 5
Debug.Write(index.ToString & " ")
Next index
The documented output is 1 2 3 4 5.
Count downward
To count down, give Step a negative value. This Visual Basic .NET example decreases by a quarter at each iteration:
Rank #3
For number As Double = 2 To 0 Step -0.25
Debug.Write(number.ToString & " ")
Next
It produces values from 2 down to 0 in quarter-step decrements.
VBA example
In VBA, the following loop uses the default step of 1 and prints each counter value:
Rank #4
For counter = 1 To 5
Debug.Print counter
Next counter
When to use For…Next instead of another loop
Use For...Next when the code should process a known numeric range or a set of iterations controlled by a counter. If the number of repetitions is not known before the loop begins—for example, if it depends on a condition becoming true—Microsoft’s Visual Basic .NET guidance recommends considering a While or Do loop instead.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteNested loops and early exit
Loops can be nested when each pass through one loop needs to run another sequence. Give each loop a distinct counter so it is clear which value changes at each level. Reusing or changing counters inside loop bodies can make the progression harder to understand.
Best Value
In Visual Basic .NET, Exit For leaves the loop before it reaches its normal ending point. Continue For skips the remaining statements in the current iteration and proceeds to the next iteration. Consult the relevant language reference for exact behavior in the dialect you are using.
What happens if loop values change inside the body?
In Visual Basic .NET, the start, end, and step expressions are evaluated when the loop begins. Changing variables used in those expressions later does not change the iteration values already determined for that loop. Modifying the counter itself inside the body can also make the sequence difficult to reason about, so prefer letting Next advance it.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.

