Solved

Can you go backwards in a For Each Loop?

Posted on 2010-09-18
7
759 Views
Last Modified: 2012-05-10
Let's say I'm going through cells in a range.

For Each cell in sheet.Range(myRange)

Open in new window


Can I go backwards if a statement is true?  In other words, return to the previous cell.

For Each cell in sheet.Range(myRange)
    If deleteRow then 
         <go back a cell>
         cell.EntireRow.Delete
    End If
Next

Open in new window


Thanks.
0
Comment
Question by:growedup
  • 4
  • 2
7 Comments
 
LVL 50

Expert Comment

by:teylyn
ID: 33710265
Hello,

you can, but for deleting rows it is better to start at the bottom and work upwards. What are you trying to achieve?

Here is your code amended.

Better alternatives can be provided if you explain your project.

cheers, teylyn
For Each cell in sheet.Range(myRange)
    If deleteRow then 
         cell.offset(-1, 0).EntireRow.Delete
    End If
Next

Open in new window

0
 

Author Comment

by:growedup
ID: 33710304
Ah, I should clarify. I intend to to do more with the row after I the previous one.

The project is basically sorting a list.  I want all the cells in the column that contain (not equal to) a certain word at the top.  My code goes through each cell, if the word is not in the cell then I move it to the bottom.  I also apply some formatting.  

I tried your solution before I asked my question and the problem is that when a row is deleted, it skips some rows because all the row numbers change.  If I am on row 4 and I delete row 3, row 4 now becomes row 3 but cell still equals row 4.

Simplistically put, I am doing this:

go through each cell in column
if deleteRow then delete the row above, set deleteRow to false
does the cell contain the keyword?
if yes, apply formatting.
if no, move to bottom of list, apply formating, set deleteRow to True
start over
0
 

Author Comment

by:growedup
ID: 33710309
I just reread your post.  So can I go backwards in a range?  (A25:A1)?  That seems like it would fix my problem.  I will try.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:growedup
ID: 33710322
That didn't work.  It still started at A1.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 200 total points
ID: 33710411
Hi, you can go backwards if you have a reference that can calculate a minus, such a row number.

For example, if your range started at row 1 and ended at row 25, you can go backwards, referencing the row number, and the Step keyword.

For lngRow = 25 To 1 Step -1
    Set Cell = Cells(lngRow, "A")
    If deleteRow then
         cell.EntireRow.Delete
    End If
Next

Regards,

Rob.
0
 

Author Comment

by:growedup
ID: 33710579
I was afraid of that.  Not a big deal, I suppose. I just have to change my code a bit.    I was hoping there was a way to do it using For Each...  Thanks for the help.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 200 total points
ID: 33710643
No, For Each doesn't support that.  You should still be able to use the counter and determine the first and last rows in your range though.

Rob.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now