Free tools Windows power users keep installed
One-click scans. No signup required.
A For…Next loop repeats a block of Visual Basic code while a counter moves from a starting value toward an ending value. The optional Step sets how the counter changes; without it, the counter increases by 1. The loop ends after the counter passes the bound, unless code exits it earlier.
How a For…Next loop works
The loop sets a counter to its starting value, runs the statements in its body, and advances the counter at Next. It repeats while the counter remains within the ending bound for the direction of travel. The start, end, and step therefore determine the values visited.
As an Amazon Associate I earn from qualifying purchases.
For example, a loop from 1 to 5 with the default step visits 1, 2, 3, 4, and 5. The final bound is included when the counter reaches it. Microsoft describes the statement as repeating statements while the loop counter approaches its final value in its Visual Basic .NET reference.
Syntax in Visual Basic .NET and VBA
The phrase “For Next loop” can refer to Visual Basic .NET or VBA. Their syntax is similar, but the counter declaration differs: Visual Basic .NET can declare a typed counter in the loop statement, while VBA’s documented form assigns a counter without that declaration.
#1 Best Overall
Visual Basic .NET
For counter [ As datatype ] = start To end [ Step step ]
statements
Next [ counter ]
A simple example prints the numbers from 1 through 5:
For index As Integer = 1 To 5
Debug.Write(index.ToString & " ")
Next index
Microsoft’s documented output is 1 2 3 4 5. See the Visual Basic .NET syntax reference.
VBA
For counter = start To end [ Step step ]
statements
Next [ counter ]
In VBA, this example prints each counter value from 1 to 5 in the Immediate window:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsFor counter = 1 To 5
Debug.Print counter
Next counter
For VBA-specific details, use Microsoft’s For…Next statement reference.
Rank #3
How Step changes the loop
Step controls the amount added to or subtracted from the counter after each pass. If omitted, its value is 1. A positive step counts upward; a negative step counts downward. Choose a step that moves toward the ending bound, or the loop may not run as intended.
For number As Double = 2 To 0 Step -0.25
Debug.Write(number.ToString & " ")
Next
This Visual Basic .NET example counts down from 2 to 0 in quarter-step decrements, including the bound. The official example and behavior are documented in Microsoft’s For…Next reference.
Rank #4
When to use For…Next instead of While or Do
Use For…Next when the work follows a known numeric range or expected iteration count, such as processing positions 1 through 10. If you do not know in advance how many repetitions are needed and the loop should continue until a condition changes, Microsoft recommends considering a While or Do loop instead.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Nested loops and early exits
You can place one For…Next loop inside another. Give nested loops distinct counters so it is clear which loop each counter controls. This also helps avoid accidentally changing the outer loop’s progress while processing the inner loop.
Best Value
In Visual Basic .NET, Exit For leaves the loop before it reaches its normal end. Continue For skips the remaining statements in the current pass and proceeds to the next iteration. See Microsoft’s Visual Basic .NET reference for these control statements.
What happens if loop inputs change?
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. Changing the counter inside the loop can also make its progression harder to follow and debug, so avoid doing so unless there is a clear reason.
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.




