Cycling through the cells in a range

W

Wandering mage

Can someone tell me how to cycle through the cells in a
range that has more than one cell selected?
 
A

Alan Beban

Wandering said:
Can someone tell me how to cycle through the cells in a
range that has more than one cell selected?
For i = 1 To Selection.Count
MsgBox Selection(i).Address
Next
Alan Beban
 
T

Tom Ogilvy

Just to add a little more explanation for the OP's benefit (not intended as
a criticism of Alan's suggested solution).
It could be as simple as the OP actually does not know how to loop through a
multicell contiguous range in which Alan's suggestion works well. I saw it
as a multicell discontiguous range in which Alan's solution would not work
(as written) but using "for each cell in selection" would work (in both
cases).

to illustrate:
Sub Tester9()
Debug.Print Selection.Address
For i = 1 To Selection.Count
Debug.Print i, Selection(i).Address
Next

End Sub

produces:
$F$22,$H$22,$F$31,$H$31
1 $F$22
2 $F$23
3 $F$24
4 $F$25

and using For each

Sub Tester10()
Debug.Print Selection.Address
i = 0
For Each cell In Selection
i = i + 1
Debug.Print i, cell.Address
Next
End Sub

produces:

$F$22,$H$22,$F$31,$H$31
1 $F$22
2 $H$22
3 $F$31
4 $H$31

just some additional information that may prove useful.
 
Top