Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Stepping through For Each...Next backwards?

Hello,
Is there a way to use a For Each...Next loop but have VBA
step through the collection from the last element to the
first? I'm trying to step through a range and delete
rows that don't meet certain criteria, and I need to work
from the bottom up. I know I can do it by counting the
rows in the range and using:

for i = lastrow to 1 step -1

But I figured it'd be less lines of code if something
existed like this:

for each cl in rng step backwards

Not a big deal if it doesn't, but figured it was worth
asking.

Thanks to all.

Mike


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Stepping through For Each...Next backwards?

Mike,

No, you cannot change the order in which a For Each loop
iterates.


"Mike Lee" wrote in message
...
Hello,
Is there a way to use a For Each...Next loop but have VBA
step through the collection from the last element to the
first? I'm trying to step through a range and delete
rows that don't meet certain criteria, and I need to work
from the bottom up. I know I can do it by counting the
rows in the range and using:

for i = lastrow to 1 step -1

But I figured it'd be less lines of code if something
existed like this:

for each cl in rng step backwards

Not a big deal if it doesn't, but figured it was worth
asking.

Thanks to all.

Mike




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Stepping through For Each...Next backwards?

Mike,

I don't think so as any range object seems to start at the earlier cell no
matter how you define it.

This code

For Each cell In Range("H10:H1")
MsgBox cell.Address
Next cell

shows H1, H2, etc, not H10, H9, etc.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Mike Lee" wrote in message
...
Hello,
Is there a way to use a For Each...Next loop but have VBA
step through the collection from the last element to the
first? I'm trying to step through a range and delete
rows that don't meet certain criteria, and I need to work
from the bottom up. I know I can do it by counting the
rows in the range and using:

for i = lastrow to 1 step -1

But I figured it'd be less lines of code if something
existed like this:

for each cl in rng step backwards

Not a big deal if it doesn't, but figured it was worth
asking.

Thanks to all.

Mike




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Stepping through For Each...Next backwards?

Didn't think so, but figured it was worth asking.

Thanks to both of you for your answers.

Mike


-----Original Message-----
Mike,

No, you cannot change the order in which a For Each loop
iterates.


"Mike Lee" wrote

in message
...
Hello,
Is there a way to use a For Each...Next loop but have

VBA
step through the collection from the last element to

the
first? I'm trying to step through a range and delete
rows that don't meet certain criteria, and I need to

work
from the bottom up. I know I can do it by counting the
rows in the range and using:

for i = lastrow to 1 step -1

But I figured it'd be less lines of code if something
existed like this:

for each cl in rng step backwards

Not a big deal if it doesn't, but figured it was worth
asking.

Thanks to all.

Mike




.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 340
Default Stepping through For Each...Next backwards?

Step through it one time, assigning the values to an array. Then step
through the array backwards.

Bob Flanagan
Macro Systems
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel

"Mike Lee" wrote in message
...
Hello,
Is there a way to use a For Each...Next loop but have VBA
step through the collection from the last element to the
first? I'm trying to step through a range and delete
rows that don't meet certain criteria, and I need to work
from the bottom up. I know I can do it by counting the
rows in the range and using:

for i = lastrow to 1 step -1

But I figured it'd be less lines of code if something
existed like this:

for each cl in rng step backwards

Not a big deal if it doesn't, but figured it was worth
asking.

Thanks to all.

Mike






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Stepping through For Each...Next backwards?

Is it a nice single column, single area range?

dim rng as range
dim i as long
set rng = activesheet.range("a1:a10")
for i = rng.cells.count to 1 step -1
msgbox rng(i).address
next i

You could also iterate backwards through areas, rows, columns. It kind of
depends on what you mean by going backwards through a range.



Mike Lee wrote:

Hello,
Is there a way to use a For Each...Next loop but have VBA
step through the collection from the last element to the
first? I'm trying to step through a range and delete
rows that don't meet certain criteria, and I need to work
from the bottom up. I know I can do it by counting the
rows in the range and using:

for i = lastrow to 1 step -1

But I figured it'd be less lines of code if something
existed like this:

for each cl in rng step backwards

Not a big deal if it doesn't, but figured it was worth
asking.

Thanks to all.

Mike


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stepping in to a cell Max Excel Discussion (Misc queries) 3 April 27th 10 02:44 AM
Stepping through Code Jim May Excel Discussion (Misc queries) 9 October 25th 07 01:40 PM
Stepping Through vs. Play CWillis Excel Discussion (Misc queries) 1 June 15th 06 05:34 PM
stepping down rows JasonMeyer Excel Programming 5 February 23rd 04 08:02 PM
Stepping through a column using FOR/NEXT Joe Mathis Excel Programming 1 December 3rd 03 06:36 AM


All times are GMT +1. The time now is 01:56 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"