Technical question: Order of for each-loops

eviMogwai

New Member
Joined
Jul 4, 2011
Messages
26
When you set up a for each-loop through a range, in what order will it chose cells?
In the case of rows/columns I hope it's from left to right and top to bottom respectively.
But how is it with other ranges, say 3x4 or 4x3?

Code:
function roulette(rRange As Range)
'rRange an arbitrary range
 
For Each cell In rRange
debug.print(cell.adress(false, false))
Next
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I've only ever seen it in that order. However if order is crucial, then you should use a couple of nested loops to go through the rows, then within each row go through the columns.
 
Upvote 0
Indeed I could, but then i wouldn't learn anything new ;)

There are some benefits to using the single for each ... next-loop with regards to memory usage and speed (from what I've gathered) but most of all I'd like to write this slightly more elegant solution (and not have to repeat a lot of code in the program I wrote it for)
 
Last edited:
Upvote 0
When you set up a for each-loop through a range, in what order will it chose cells?
In the case of rows/columns I hope it's from left to right and top to bottom respectively.
But how is it with other ranges, say 3x4 or 4x3?
Yes you're right it's left to right.. tried and tested..
see example..
Code:
sub chkpos()
 
for each c in range("A2:D10")
    msgbox c
next c
end sub
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top