Working with individual cells in a range

Once you've got hold of a range, you might want to do something with each cell individually. For example, to check the contents of each cell.

There are two notations, a (row, col) coordinate or the nth item in the range. The row, col notation is convenient if you are working with a set range. For example:

MsgBox Range("a1..b5")(1, 1).Value 'displays contents of cell a1

MsgBox Range("a1..b5")(2, 2).Value 'displays contents of cell b2.

The alternative notation takes a range from left to right, top to bottom and numbers it from 1 to n. So the exact equivalent of the above is:

MsgBox Range("a1..b5")(1).Value 'displays contents of cell a1

MsgBox Range("a1..b5")(2).Value 'displays contents of cell b2.

But really this notation is more useful when you don't know in advance the extent of a range (for example, dealing with a selection). You can be sure to see every item in a range using the following:

For i = 1 to Selection.Count

MsgBox Selection(i).Value 

Next