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!
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
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>
 

bill

Well-known Member
Joined
Mar 7, 2002
Messages
550
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!
 

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
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?
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473

ADVERTISEMENT

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:
 

bill

Well-known Member
Joined
Mar 7, 2002
Messages
550
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.
 

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962

ADVERTISEMENT

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.
 

bill

Well-known Member
Joined
Mar 7, 2002
Messages
550
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.
 
L

Legacy 21301

Guest
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,125
Messages
5,599,843
Members
414,342
Latest member
K Darrell Smith

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