The Analyst's Path

VBA loops

A loop is the "repeat this" quantifier. For Next runs a known number of times, and For Each visits every member of a collection. Do Loop keeps going while or until a condition holds, so use it when you don't know in advance how many passes you need. The most common off-by-one trap in VBA is forgetting that the last value gets processed too. The one real hazard of Do is the infinite loop.

3 min readPart of VBA II: Logic, Functions & EventsOpen in course mode

Advertisement

A loop is the "repeat this" quantifier. VBA gives you three, and each one answers a different question.

For…Next, "do this a known number of times / walk an index". Use it when you know the bounds, especially to index into rows, columns, or an array:

Dim i As Long, total As Double
For i = 2 To 12
    total = total + Cells(i, "C").Value
Next i

For i = 2 To 12 runs with i = 2,3,…,12, inclusive of both ends. The most common off-by-one trap in VBA is forgetting that the last value gets processed too. The Step keyword changes the stride. Counting down with For i = 100 To 2 Step -1 is what you need whenever you delete rows, so later rows don't shift under you. A Step 2 skips every other one. Exit For bails out early, so the moment you've found what you need or hit a sentinel, you stop:

Advertisement

For i = 2 To 1000
    If Cells(i, "A").Value = "" Then Exit For   ' stop at first blank
    total = total + Cells(i, "B").Value
Next i

For Each…Next, "do this to every member of a collection". Use it when you don't care about the index and only want to visit each item, whether that's every cell in a range, every worksheet in a workbook or every key in a Dictionary:

Dim c As Range, hits As Long
For Each c In Range("B2:B100")
    If c.Value > 10000 Then hits = hits + 1
Next c

For Each is cleaner and often faster for object collections. It never has an off-by-one bound, because there's no counter. It simply stops when the collection runs out. The loop variable must be the right type: Range for cells, Worksheet for sheets, Variant when iterating a Dictionary's keys.

How does a Do loop work?

Do…Loop, "keep going while / until a condition". Use it when you don't know in advance how many iterations you need. Reading down a column until the first blank is one example, and it's the one below. Retrying until success and pumping a queue until it's empty work the same way:

Dim r As Long
r = 2
Do While Cells(r, "A").Value <> ""
    Cells(r, "D").Value = Cells(r, "B").Value * Cells(r, "C").Value
    r = r + 1
Loop

Do While condition … Loop tests before each pass, so it may run zero times. A Do … Loop Until condition tests after, so it always runs at least once. Do While x and Do Until Not x are equivalent, so pick whichever reads as the plain-English intent ("while there is data" vs "until we hit a blank"). Exit Do breaks out early, just like Exit For. The one real hazard of Do is the infinite loop. Forget to advance r = r + 1 and the condition never changes, so Excel hangs. Ctrl+Break is your escape hatch.

Advertisement

Part of the course

This page is part of VBA II: Logic, Functions & Events (EX6.02), in Phase 6: Investing philosophy, mental models and behavioural edge. The module's flashcards and its quiz are in the app.