PDA

View Full Version : simple loop backwards



Djblois
01-25-2007, 10:07 AM
I am trying to do a simple loop up to the top of a worksheet but I can never get it to work. It isn't for my add-in so I don't want anything complicated. It is just for a small little project, I need to get done today.

here is the code:


For i = finalRow To 2

If Range("E" & i).Value = "" Then
ActiveCell.EntireRow.delete
Else:
End If
Next


It is supposed to loop through the report and delete the line if the cell is blank but I can't get it to work.

OBP
01-25-2007, 10:15 AM
Djblois, you need to add
step -1
after the 2 on the For statement.
Like so
For i = finalRow To 2 Step -1

Djblois
01-25-2007, 10:19 AM
thank you

Bob Phillips
01-25-2007, 10:30 AM
I am trying to do a simple loop up to the top of a worksheet but I can never get it to work. It isn't for my add-in so I don't want anything complicated. It is just for a small little project, I need to get done today.

here is the code:


For i = finalRow To 2

If Range("E" & i).Value = "" Then
ActiveCell.EntireRow.delete
Else:
End If
Next

It is supposed to loop through the report and delete the line if the cell is blank but I can't get it to work.

Why would you be deleting the row of the activecell, should it be the row pointed tgo by i?



Rows(i).delete

mdmackillop
01-25-2007, 12:03 PM
You can also do this without a loop using SpecialCells

Sub Macro1()
Range(Cells(2, 5), Cells(Rows.Count, 5).End(xlUp)) _
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Djblois
01-25-2007, 12:42 PM
cool thank you