Ad space (banner)
Lesson 14 / 20

Looping over cells (For Each)

This lesson covers For Each, which processes every element of a cell range or collection one at a time.

Writing For Each variable In collection ... Next repeats the same work for each element of a cell range or collection. What is distinctive is that you never have to think about which element you are on.

The sample code totals the values with For Each (this lesson assumes the values are already set).

A common early stumble is choosing between For and For Each. A For statement suits a numeric range such as "1 to 10"; For Each suits every element of a cell range or collection.

In professional work, For Each comes up constantly for checking every cell in a particular column.

๐Ÿ“– 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)