VBA Remove rows based on date

osvald

New Member
Joined
Sep 18, 2014
Messages
3
Hi!

I want to delete rows that has a date in column H that is 7 days older than today.
Preferably I'd like the macro to run when someone open the worksheet (possible with Private Sub Workbook_Open()?).
So that it automaticly wipe away rows that's not relevant any more.

Is this possible? Can anyone help me with this?
I'd greatly appreciate it.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I've tried this code and it almost does the work. But it removes empty cells also, not just the cells with date 7 days prior today.
Any suggestions?

Code:
Sub Delete_Prior()
     
    Dim i As Long
     
    For i = Range("J" & Rows.Count).End(xlUp).Row To 9 Step -1
        If Range("J" & i) < Date - 7 Then
            Range("J" & i).EntireRow.Delete
        End If
    Next i
End Sub

(The column is J not H as stated in the thread start)
 
Upvote 0
Try

Rich (BB code):
Sub Delete_Prior()
     
    Dim i As Long
     
    For i = Range("J" & Rows.Count).End(xlUp).Row To 9 Step -1
        If Range("J" & i).Value <> "" And Range("J" & i).Value < Date - 7 Then
            Range("J" & i).EntireRow.Delete
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,783
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top