[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Can you go backwards in a For Each Loop?

Posted on 2010-09-18
7
Medium Priority
?
822 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

Accepted Solution

by:
RobSampson earned 800 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 800 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

649 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