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!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
There are a few ways to do it. My favorite is like this:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Backwards()

<SPAN style="color:#00007F">Dim</SPAN> Counter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

    <SPAN style="color:#00007F">For</SPAN> Counter = Range("A65536").End(xlUp).Row <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1
        Range("A" & Counter).Value = Range("A" & Counter).Value & " 1"
        <SPAN style="color:#007F00">'Adds a space and the number one after the value of each cell</SPAN>
        <SPAN style="color:#007F00">'from the last used cell in column A to cell A1</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> Counter
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thansk for the reply.

The solution you suggest is kinda what I wanted to avoid.
That is iterating through the range by row number backwards.

Your marque is pretty neat however!
 
Upvote 0
Duh. Silly me. I'm not sure if you can do a For....Each in your case. I'm curious, why do you want to use For....Each?
 
Upvote 0
bill said:
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!

Is there a good reason to be obtuse? :LOL:
 
Upvote 0
Well, FOR EACH is so much sweeter.
I know, who cares right?

Um, not sure about the obtuse remark!

My plan was to pass a range to a sub that removed dups.
Going forward always seems to place my current cell ahead of the last cell AFTER a row within the range was deleted.

Going backwards alwasy seems to work OK.
 
Upvote 0
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.
 
Upvote 0
It sounds like you are just trying to sharpen your tools and know the limits of your options.

I suppose that's so however using a FOR EACH is a bit more than you imply. For me anyway.

Thanks for yor confirmation however.
 
Upvote 0
I suppose you could use :-

Dim rng As Range, i#, cell As Range
Set rng = [A1:A10]
i = rng.Cells.Count
For Each cell In rng
rng(i) = rng(i).Value & " 1"
i = i - 1
Next

But this is doing the same thing as the other code - and doesn't have any advantage over the other way.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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