# Iterating through a range backwards

#### bill

##### Well-known Member
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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

#### phantom1975

##### MrExcel MVP
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

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

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?

#### bill

##### Well-known Member
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

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

#### bill

##### Well-known Member
lol

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,168,037
Messages
5,856,960
Members
431,841
Latest member
jaybeem

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

### Which adblocker are you using?

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

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