Filter and delete rows based on date prior to Current Month

Pat_The_Bat

Board Regular
Joined
Jul 12, 2018
Messages
83
Trying to delete any rows that have a date prior to the 1st day of the current month. When I debug.print my variable MTD it gives the date 8/1/2018, but then when I filter and delete rows it goes haywire. Any advice?




Code:
Sub MTDapps()
With Sheets("Table")
    Rows.Sort Range("J2"), order1:=xlDescending, Header:=xlYes
    End With
    
Dim MySheet As Worksheet
Dim LastRow As Long, LastCol As Long
Dim MyRange As Range


Application.DisplayAlerts = False


Dim m As Integer
Dim y As Integer
Dim MTD As Date


m = month(Now)
y = year(Now)


MTD = m & "/1/" & y


Debug.Print m
Debug.Print y
Debug.Print MTD




Set MySheet = ThisWorkbook.Worksheets("Table")




With MySheet
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    LastCol = .Range("A" & .Columns.Count).End(xlToLeft).Column
    
    Set MyRange = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
End With


With MyRange
    .AutoFilter Field:=1, Criteria1:="<" & MTD
    
       
    


    '.SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count).Rows.Delete
End With




    


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You can use DateSerial function.

Code:
myDate = DateSerial(Year(Date), Month(Date), 1)
 
Upvote 0
You can use DateSerial function.

Code:
myDate = DateSerial(Year(Date), Month(Date), 1)


Same result. Were you indicating that the way I've established the value for MTD is the problem? Whether I set MTD as the control date by

Code:
MTD = DateSerial(year(Date), month(Date), 1)

or with


Code:
MTD= m & "/1/" & y

either way the variable is set to 8/1/2018 but I still don't get the correct result from the macro. The spreadsheet basically appears to have a filter and no data showing. It's not filtering out any row where the date in column J is prior to 8/1/2018 as expected.
 
Upvote 0
Ok lets try converting the date to an integer. If you have dimensioned the variable change that to Long. See if this filters correctly:

Code:
myDate = CLng(DateSerial(Year(Date), Month(Date), 1))
 
Upvote 0
Hi, This should work. (Did for me)

sDate = "<" & Format(Date - Day(Now) + 1, "mm/dd/yyyy")

With MyRange
.AutoFilter Field:=1, Criteria1:=sDate


Also, out of interest, is there a reason why you use a range and not ActiveSheet.UsedRange.AutoFilter?

I'm still getting to grips with the best way of doing things.


Cheers

Ray
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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