Solved

Can you go backwards in a For Each Loop?

Posted on 2010-09-18
7
805 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 50
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

734 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