Cycling through the cells in a range

  • Thread starter Thread starter Wandering mage
  • Start date Start date
W

Wandering mage

Can someone tell me how to cycle through the cells in a
range that has more than one cell selected?
 
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top