Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

What Is a For…Next Loop in Visual Basic? Definition and Examples

A For…Next loop repeats Visual Basic statements as a counter moves through a defined range. See the syntax, Step behavior, and examples for Visual Basic .NET and VBA.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
For counter = 1 To 5
    Debug.Print counter
Next counter

For VBA-specific details, use Microsoft’s For…Next statement reference.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.