ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Cycling through the cells in a range (https://www.excelbanter.com/excel-programming/301620-cycling-through-cells-range.html)

Wandering Mage

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

Tom Ogilvy

Cycling through the cells in a range
 
for each cell in Selection
msgbox cell.Address
Next

--
Regards,
Tom Ogilvy

"Wandering mage" wrote in message
...
Can someone tell me how to cycle through the cells in a
range that has more than one cell selected?




Wandering Mage

Cycling through the cells in a range
 
Thank you.
-----Original Message-----
for each cell in Selection
msgbox cell.Address
Next

--
Regards,
Tom Ogilvy

"Wandering mage"

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



.


Alan Beban[_2_]

Cycling through the cells in a range
 
Wandering mage wrote:

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

Tom Ogilvy

Cycling through the cells in a range
 
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.

--
Regards,
Tom Ogilvy


"Alan Beban" wrote in message
...
Wandering mage wrote:

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





All times are GMT +1. The time now is 07:25 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com