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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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