Ad space (banner)
Lesson 5 / 20

Repeating (the For statement)

This lesson covers looping in VBA with a For statement.

Writing For variable = start To finish ... Next repeats a block a fixed number of times.

The sample code writes For i = 1 To 5 ... Next i to join the numbers 1 through 5 into a message one at a time.

A common early stumble is forgetting or mistyping the loop variable after Next. Writing Next i makes it obvious which loop is ending.

In professional work, For statements are used very heavily to process a range of cells row by row.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)