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

jgome21

New Member
Joined
Nov 26, 2017
Messages
13
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 of the current month (i.e. for Jan. I would like to delete all rows that reference a date of 12/31/17 or prior). So far I have this, but I am currently stuck. Any tips would be appreciated. Thanks.

Sheets("3. Docs Charged").Select
With Sheets("3. Docs Charged")
lastrow = Cells(Rows.Count, 14).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 14).Value2 < Date Then Rows(i).EntireRow.Delete
Next i
End With
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Do you want it limited to just the one month prior to the current month (i.e. only December 2017 right now), or do you want it to delete ANY month prior to the current one?
 
Upvote 0
Try this:
Code:
Sub MyDeleteRows()

    Dim lastrow As Long
    Dim y As Long
    
    Sheets("3. Docs Charged").Select
    With Sheets("3. Docs Charged")
        lastrow = Cells(Rows.Count, 14).End(xlUp).Row
        For i = lastrow To 2 Step -1
            If (Cells(i, 14).Value2 < Date) And _
                ((Month(Cells(i, 14)) < Month(Date)) Or _
                (Year(Cells(i, 14)) < Year(Date))) _
                Then Rows(i).EntireRow.Delete
        Next i
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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