VBA code for deleting rows if cell references date of prior month or prior year from current month and current year.

Job1997

New Member
Joined
Oct 7, 2020
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Guys,

I am having trouble writing a VBA code that would delete all rows if a cell contains a date of the prior month and prior year of the current month and current year . So far I have this, but I am currently stuck. Any tips would be appreciated. Thanks.

It concerns the cell B


Sub MyDeleteRows()

Dim lastrow As Long
Dim y As Long

Sheets("Sheet1").Select
With Sheets("Sheet1")
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
For i = lastrow To 2 Step -1
If (Cells(i, 2).Value2 < Date) And _
((Month(Cells(i, 2)) < Month(Date)) Or _
(Year(Cells(i, 2)) < Year(Date))) _
Then Rows(i).EntireRow.Delete
Next i
End With

End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    55.6 KB · Views: 19

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello, so basically you require the data of the current month for this year or current month of all years eg October month data of 2020 or October month data of 2019,2018,2020 ??
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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