Loop through cell in reverse order

marcus.brown

Board Regular
Joined
Jun 23, 2008
Messages
61
Hi,

I'm creating a function which does some calculation. The parameter for this function is a range. I need to loop through the range specified by the user in reverse order when doing my calculations. The calculations are relatively straightforward, it is summing up some figures, but stops once it has reached upto a certain value, but it needs to some these figures up in the reverse order.

Can someone help me with how to look through a range in reverse order?

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Gary McMaster

Well-known Member
Joined
Feb 8, 2009
Messages
1,977
The first thing you need to know is if your definition of reverse order is the same as Excel's. It stores the cells in the range from left to right, top to bottom.

Below is sample that will read through the range in reverse order as stored by Excel.

(select a small grid of cells)

Gary

Code:
Public Sub Test()

Dim oRange As Range
Dim lCount As Long

Set oRange = Selection

For lCount = oRange.Cells.Count To 1 Step -1
    oRange.Cells(lCount).Interior.ColorIndex = 3
    MsgBox "Pause"
Next lCount

End Sub
 

marcus.brown

Board Regular
Joined
Jun 23, 2008
Messages
61
Hi,

Thanks! I'm now having a problem whereby i don't know how to get the value of the cell that it is currently at.

Normally, in a for next loop (like below), i would do cell.value to obtain this value. When doing this now, it doesn't work.

For each cell in oRange

cell_Value = cell.value

Next

Thanks
 

mancubus

Active Member
Joined
Mar 7, 2010
Messages
342
try
Code:
For each cell in oRange
     MsgBox cell.value
Next

try
Code:
For each cell in oRange
     cell_value = cell.value
     MsgBox cell_value
Next
 
Last edited:

Gary McMaster

Well-known Member
Joined
Feb 8, 2009
Messages
1,977

ADVERTISEMENT

It's very similar:

Code:
cell_value = oRange.Cells(lCount).Value

Gary
 

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196
Hi,

Thanks! I'm now having a problem whereby i don't know how to get the value of the cell that it is currently at.

Normally, in a for next loop (like below), i would do cell.value to obtain this value. When doing this now, it doesn't work.

For each cell in oRange

cell_Value = cell.value

Next

Thanks
Probably best not to use reserved words for variable names - it just confuses the issue.

Code:
dim thisCell as range
For each thisCell in oRange
 
cell_Value = thisCell.value
 
Next
 

Watch MrExcel Video

Forum statistics

Threads
1,130,046
Messages
5,639,753
Members
417,108
Latest member
Thein Than

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
Top