Excel - Macro to delete rows based on date

Asked By PMB on 18-Feb-09 07:27 AM
I have a spreadsheet with completion dates in column C.  I want my macro to
look at each date, check to see if it's older than 30 days, and then delete
the rows that are older than 30 days.  I have a macro that seems to work, but
I can't get it to loop (and I tried many times to make that happen).  I'm
missing the loop piece, but also want to make sure I've put the "formula"
part in properly. Here's my macro:

Public Sub Delete1()
If ActiveCell.Value <= "TODAY()-30" Then
ActiveCell.Offset(1, 0).Select
End If
End Sub


Mike replied on 18-Feb-09 07:52 AM

Public Sub Delete1()
lastrow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
For x = lastrow To 3 Step -1
If Cells(x, 3).Value <= Date - 30 Then
Cells(x, 3).EntireRow.Delete
End If
End Sub

PMB replied on 18-Feb-09 08:22 AM
PERFECT!  No wonder you are gold level.  Thank You.
Mike replied on 18-Feb-09 08:42 AM
Your welcome, glad I could help
Don Guillett replied on 18-Feb-09 08:50 AM
You may like this to do all without a loop. I have used line continuation _
to prevent word wrap

Sub filterdates()
lr = Cells(Rows.Count, "a").End(xlUp).Row
Range("c1:c" & lr).AutoFilter Field:=1, _
Criteria1:="<" & Date - 30
Range("c2:c" & lr) _
Range("c1:c" & lr).AutoFilter
End Sub

Don Guillett
Microsoft MVP Excel
SalesAid Software