VBA Loops in Excel: When to Use For Next and Do While
Use For Next for a known number of rows and Do While to scan to a stopping condition. The key difference is how each loop advances and tests.

Loops let an Excel VBA macro repeat an instruction without requiring a separate line of code for every row. For a fixed number of steps, For...Next handles the counting. When the number of rows depends on the data, Do While...Loop can keep going until it reaches a stopping condition. Choosing between them comes down to what you know before the macro starts.

That distinction matters when you scale up a small macro. A routine that writes values into three cells can repeat its assignment three times. Doing the same for 100 rows would mean maintaining 100 similar lines. A loop keeps the work in one place and changes the row number on each pass, or iteration.

VBA offers different loop structures for different stopping rules. For...Next is suited to a known count, such as writing to the first 10 rows. Do While...Loop suits a scan that should continue while a test remains true, such as checking a column until it reaches an empty cell.
Use For Next when the count is known
In this example, the macro writes the numbers 1 through 10 in column A. The variable i serves as both the row number and the value placed in that row:

Sub WriteTenCells()
For i = 1 To 10
Cells(i, 1) = i
Next i
End Sub
The For line sets the starting value to 1 and the ending value to 10. On the first pass, Cells(i, 1) = i writes 1 to row 1 of column 1, which is cell A1. When VBA reaches Next i, it increases i by 1 and returns to the loop body. The next pass writes 2 to A2, and the pattern continues through A10.
VBA handles that increment automatically in a For...Next loop. After the pass for i = 10, the loop ends and execution continues with the line after Next i. To extend this example to 1,000 rows, change the ending value to 1,000. The assignment itself stays the same.
This structure is a good fit when the start and end values are part of the task: for example, processing a set number of rows or repeating an operation a set number of times. The loop’s bounds make the intended number of passes visible in the code.

Use Do While when the end depends on the data
A column’s length may vary from one worksheet to another. In that case, a macro can test each row and continue while the cell in column A contains a value. The example below assumes column A already contains the values 1 through 10. It reads each value, writes twice that value in column B, and stops when it finds an empty cell in column A:
Sub DoubleValues()
i = 1
Do While Cells(i, 1) <> ""
Cells(i, 2) = Cells(i, 1) * 2
i = i + 1
Loop
End SubHere, i = 1 sets the starting row before the loop begins. The test Cells(i, 1) <> "" means that the loop continues while the current cell in column A is not empty. The assignment writes the doubled value to column B on the same row. Then i = i + 1 advances the macro to the next row.

That increment is essential. Unlike For...Next, a Do While loop does not increase the counter for you. If the code omits i = i + 1, it keeps checking the same cell. If that cell is nonempty, the stopping test remains true and the loop can run indefinitely. The source material warns that stopping a stuck macro may require Ctrl+Break, a key combination that can be awkward on laptops without a Break key. Check that every loop path advances the row or otherwise reaches its stopping condition.
The two examples also do different jobs. The For...Next macro generates values as it runs. The Do While macro processes values that are already present in column A. Keeping that distinction clear helps when adapting either pattern to a worksheet: first decide whether the macro knows the number of passes, or whether it needs to inspect the data to find where to stop.
Choose when the condition is tested
A Do While condition can appear at the beginning or the end of the loop. Those forms change whether the body runs when the condition is false at the outset:
Do While Cells(i, 1) <> ""
...
Loop
Do
...
Loop While Cells(i, 1) <> ""With Do While...Loop, VBA checks the condition before each pass. If the first cell is empty, the body does not run. With Do...Loop While, VBA runs the body once before checking the condition, so the body always gets at least one pass. That difference matters when a macro must not process anything unless the first cell passes the test.
Declare the counter with care
Loop examples often use a counter named i. A declaration such as Dim i As Integer sets the variable’s type, but the source notes that Integer is an older type that can cause a macro to fail on a large worksheet. The related discussion of VBA variable types is available in VBA Variables: Why the AI Still Uses Integer and Single.
Before adapting a loop, check its starting value, stopping rule, and increment. For a fixed range, set the For...Next bounds. For a scan of variable length, make sure the Do While test matches the worksheet data and the counter advances on every pass. The next course in the sequence covers declaring variables in VBA.