Iterating through a range backwards

bill

Well-known Member
Joined
Mar 7, 2002
Messages
550
Using a FOR EACH, can you iterate through a range backwards?

I know I can do it using:
FOR THISROW = BOTTOMROW TO TOPROW STEP - 1
but that is so blunt!
 
Hi,

This isnt much use, but satisfies your criteria:

Code:
Sub xxx()
Dim R As Range

For Each R In Range("A8,A7,A6,A5,A4,A3,A2,A1")
    MsgBox R.Address
Next R
End Sub

HTH

Alan
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
How about

Code:
Sub xxx()
Dim R As Range
Dim rngA As Range

Set rngA = Range("A2:A11")

For Each R In rngA
    MsgBox Cells((rngA.Rows.Count + rngA.Row + 1) - R.Row, 1).Address
Next R
End Sub

Alan
 
Upvote 0
al_b_cnu said:
How about

Code:
Sub xxx()
Dim R As Range
Dim rngA As Range

Set rngA = Range("A2:A11")

For Each R In rngA
    MsgBox Cells((rngA.Rows.Count + rngA.Row + 1) - R.Row, 1).Address
Next R
End Sub

Alan


Shouldn't that be :

MsgBox Cells(rngA.Rows.Count + 2 * rngA.Row - 1 - R.Row, 1).Address
 
Upvote 0
al_b_cnu said:
This isnt much use, but satisfies your criteria:
Code:
For Each R In Range("A8,A7,A6,A5,A4,A3,A2,A1")
    MsgBox R.Address
Next R

Perhaps one could argue that my criteria is satisifed but as far as I can see, the range you've defined is still being processed from the 1st to last element.

No matter however as I appreciate your response.
 
Upvote 0
phantom1975 said:
Any time you are going to remove/add/insert rows/columns, I suggest going backward for that very reason. It sounds like you are just trying to sharpen your tools and know the limits of your options. I'm afraid that For...Each is limited to going forward.

You know, with a little bit of mustard, eating my words isn't so bad. I completely forgot.....you really can do just about anything with VB. It just depends on how willing you are to put something together.
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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