ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For/Next Is Not Looping - Only Deletes First Instance (https://www.excelbanter.com/excel-programming/430515-next-not-looping-only-deletes-first-instance.html)

Safari

For/Next Is Not Looping - Only Deletes First Instance
 
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

marcus[_3_]

For/Next Is Not Looping - Only Deletes First Instance
 
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


Safari

For/Next Is Not Looping - Only Deletes First Instance
 
On Jun 29, 7:50*pm, marcus wrote:
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!!!!


All times are GMT +1. The time now is 01:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com