For/Next Is Not Looping - Only Deletes First Instance

S

Safari

This code compares dates in range A5:A500 if any of them are greater
than the value in A1 it deletes that row.
It only deletes the first row that meets this criteria, then it
stops. If I copy the block of code twice, it works on the first two
rows. How can I make this loop?

Dim c As Range, compdate As Date
compdate = Range("A1").Value

If IsDate(compdate) Then
For Each c In Range("A5:A500", Range("K65536").End(xlUp))
If IsDate(c.Value) And c.Value > compdate Then
c.EntireRow.Delete
End If
Next

End If
 
M

marcus

Hi Safari

Starting at the bottom and moving backwards will help. Something like
this should do the trick.

Take Care

Marcus

Sub DelGreaterThan()

Dim i As Integer
Dim CompDate As Date
CompDate = Range("A1").Value

For i = 500 To 5 Step -1
If Range("A" & i).Value > CompDate Then
Range("A" & i).EntireRow.Delete
End If
Next i

End Sub
 
S

Safari

Hi Safari

Starting at the bottom and moving backwards will help.  Something like
this should do the trick.

Take Care

Marcus

Sub DelGreaterThan()

Dim i As Integer
Dim CompDate As Date
CompDate = Range("A1").Value

For i = 500 To 5 Step -1
    If Range("A" & i).Value > CompDate Then
        Range("A" & i).EntireRow.Delete
    End If
Next i

End Sub

Brilliant.

Thanks so much Marcus!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top