Excel like a finance pro.
VBA · Control flow
For ... Next
Repeats a block a set number of times with a counter.
When to use it
For ... Next repeats a block a set number of times with a counter, the standard loop for rows and columns.
The code
- Code
For i = 2 To lastRow Cells(i, 3).Value = Cells(i, 2).Value * 2 Next i
Worked examples
Loop over rows
For i = 2 To lastRow Cells(i, 3).Value = Cells(i, 2).Value * 2 Next i → Doubles column B into C for every data row
Row loop.
Delete rows
For i = lastRow To 2 Step -1 If Cells(i, 1).Value = "" Then Rows(i).Delete Next i → Deletes blank rows safely from the bottom up
Delete backwards so indexes stay valid.
Step
For i = 1 To 10 Step 2 → i = 1, 3, 5, 7, 9
Step sizes.
Worth knowing
- Find lastRow once before the loop, not inside it.
- Use Long for the counter.
- Exit For leaves early.
Where it goes wrong
- Deleting rows in a forward loop skips rows; loop backwards.
- Changing the counter inside the loop is allowed but confusing.
Related
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.