PDA

View Full Version : Solved: Expression not running all the way through



Klartigue
06-06-2012, 11:58 AM
I have this expression to delete the rows in which the cell in column D has the value "0" in it.

However this expression only deletes some of the rows where cell D is "0".

Do you see anything wrong with it?

Sub tester2()

Dim lastrow As Long
Dim i As Long
With ActiveSheet

lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow

If .Cells(i, "D").Value = "0" Then

With Range(.Cells(i, "A"), .Cells(i, "X")).Select
Selection.Delete
End With

End If

Next i

End With

End Sub

mikerickson
06-06-2012, 12:32 PM
When deleting rows, one has to work from the bottom up
For i = lastrow To 2 Step -1

Klartigue
06-06-2012, 12:35 PM
Sub tester2()

Dim lastrow As Long
Dim i As Long
With ActiveSheet

lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = lastrow To 2 Step -1

If .Cells(i, "D").Value = "0" Then

With Range(.Cells(i, "A"), .Cells(i, "X")).Select
Selection.Delete Shift:=xlUp
End With

End If

Next i

End With

End Sub

that works great, thanks!!!