Solved

Can you go backwards in a For Each Loop?

Posted on 2010-09-18
7
783 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:Ingeborg Hawighorst
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

770 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