Macro to delete entire row if date in column G is older than the current month a year ago

AnthoHead

New Member
Joined
Jul 20, 2014
Messages
15
I need to figure out how to change the below macro to delete all dates from 1 year ago prior to the month we are currently in. For example, because today is March 17th, the below will delete all rows prior to March 17th 2022, but I want all of March. I want it to delete everything prior to March vs. prior to March 17th.

Any suggestions?

Sub DeleteDateOlderThan365()
Dim lR As Long, R As Range, i As Long
lR = Range("G" & Rows.Count).End(xlUp).Row
Set R = Range("G1:G" & lR)
Application.ScreenUpdating = False
For i = lR To 1 Step -1
If IsDate(R.Cells(i)) Then
If Date - R.Cells(i).Value > 365 Then
R.Rows(i).EntireRow.Delete
End If
End If
Next i
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,
You could test Date-Day(Date) :
VBA Code:
Sub DeleteDateOlderThan365()
Dim lR As Long, R As Range, i As Long
lR = Range("G" & Rows.Count).End(xlUp).Row
Set R = Range("G1:G" & lR)
Application.ScreenUpdating = False
    For i = lR To 1 Step -1
        If IsDate(R.Cells(i)) Then
            If Date - Day(Date) - R.Cells(i).Value > 365 Then
                R.Rows(i).EntireRow.Delete
            End If
        End If
    Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Just another option
VBA Code:
Sub DeleteDateOlderYearAgo()
    Dim i As Long, lR As Long
     Application.ScreenUpdating = False
     
    lR = Range("G" & Rows.Count).End(xlUp).Row

    For i = lR To 1 Step -1
        If IsDate(Cells(i, "G")) Then
            If Cells(i, "G") < DateSerial(Year(Date) - 1, Month(Date), 1) Then
                Rows(i).Delete
            End If
        End If
    Next i
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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