AutoFilter with Dates - Date range, yes; single date, no

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
Office Version
  1. 2019
I'm trying to filter dates in column A that are in the format 09-Jul-24.
The following code will not return any records for a single date (9th of July), but will correctly filter for the entire month of interest (including 12 records for the 9th of July).
What am I doing wrong?

VBA Code:
Sub TestDayFilter()

Dim EarningsDay As Long, StartDate As Long, EndDate As Long

EarningsDay = Sheets("Invoicing").Range("L1").Value

With Sheets("Invoicing").Range("L1")
        StartDate = DateSerial(Year(.Value), Month(.Value), 1)
        EndDate = DateSerial(Year(.Value), Month(.Value) + 1, 0)
        
End With

Sheets("Operations").Select

If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
End If
    
Selection.AutoFilter Field:=1, Criteria1:=EarningsDay
Selection.AutoFilter Field:=1, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Please post a sample of a data set that is not working as it should, so we can set it up exactly as you have it and try it for ourselves, and see the issue in action.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hello Joe. Here's a sanitised dataset.
Book1
ABCDEFGHIJKLMNOPQ
1Volume (L)Storage$0.0023.73$1,574.50
2DateVLBlBaODB4AfterOffOnAddnRateg/mL/#PriceTimeCost
316-Jun-240.33$20.00
420-Jun-240.17$10.00
520-Jun-240.10$6.00
620-Jun-240.17$10.00
728-Jun-241.50$90.00
828-Jun-241.25$75.00
928-Jun-240.33$20.00
1003-Jul-240.10$6.00
1103-Jul-240.10$6.00
1203-Jul-240.10$6.00
1303-Jul-240.10$6.00
1403-Jul-240.10$6.00
1503-Jul-240.10$6.00
1603-Jul-240.10$6.00
1703-Jul-240.10$6.00
1803-Jul-240.10$6.00
1903-Jul-240.10$6.00
2003-Jul-240.10$6.00
2103-Jul-240.17$10.00
2203-Jul-24$37.50
2304-Jul-240.10$6.00
2404-Jul-240.10$6.00
2504-Jul-240.10$6.00
2604-Jul-240.10$6.00
2704-Jul-241.33$80.00
2804-Jul-242.08$125.00
2905-Jul-240.10$6.00
3005-Jul-240.10$6.00
3105-Jul-241.67$100.00
3205-Jul-240.10$6.00
3305-Jul-240.10$6.00
3405-Jul-240.10$6.00
3505-Jul-240.10$6.00
3605-Jul-240.75$45.00
3705-Jul-240.10$6.00
3805-Jul-240.10$6.00
3906-Jul-241.45$87.00
4006-Jul-240.10$6.00
4106-Jul-240.10$6.00
4206-Jul-242.00$120.00
4306-Jul-24 
4406-Jul-24 
4506-Jul-24 
4606-Jul-240.10$6.00
4706-Jul-240.10$6.00
4806-Jul-240.10$6.00
4906-Jul-240.10$6.00
5006-Jul-241.00$60.00
5106-Jul-240.10$6.00
5206-Jul-240.75$45.00
5306-Jul-241.50$90.00
5406-Jul-24 
5506-Jul-240.10$6.00
5606-Jul-240.25$15.00
5706-Jul-240.50$30.00
5807-Jul-241.00$60.00
5909-Jul-240.50$30.00
6009-Jul-240.50$30.00
6109-Jul-240.50$30.00
6209-Jul-240.10$6.00
6309-Jul-240.10$6.00
6409-Jul-240.10$6.00
6509-Jul-240.10$6.00
6609-Jul-240.17$10.00
6709-Jul-240.10$6.00
6809-Jul-24$88.00
6909-Jul-240.17$10.00
7009-Jul-24$25.00
Operations
Cell Formulas
RangeFormula
O1O1=AGGREGATE(9,7,O3:O99980)
P1:Q1P1=SUBTOTAL(9,P3:P999735)
P3,P9P3=2/6
P4,P69,P66,P21,P6P4=1/6
Q69,Q23:Q67,Q3:Q21Q3=IF(P3<>0,IF(F3="Winery Hire",25*P3,IF(OR(C3="H&L",C3="HFW"),40*P3,IF(C3<>"H&L",60*P3,""))),"")
P27P27=1+2/6-1/6+1/6
P28P28=2+5/60
P31P31=1+4/6
P36P36=25/60+2/6
Q68Q68=11*8

Book1
L
109-Jul-24
Invoicing
 
Upvote 0
Try making the changes to your code as shown here (changes in red):
Rich (BB code):
Sub TestDayFilter()

Dim EarningsDay As Long, StartDate As String, EndDate As String

EarningsDay = Sheets("Invoicing").Range("L1").Value

With Sheets("Invoicing").Range("L1")
        StartDate = Format(DateSerial(Year(.Value), Month(.Value), 1), "m/d/yyyy")
        EndDate = Format(DateSerial(Year(.Value), Month(.Value) + 1, 0), "m/d/yyyy")
        
End With

Sheets("Operations").Select

If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
End If
    
Selection.AutoFilter Field:=1, Criteria1:=EarningsDay
Selection.AutoFilter Field:=1, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate

End Sub
 
Upvote 0
Thanks for looking at this Joe. The problem step in my code is the filter for a single date. The date range filter works fine as-is. Running the code you supplied produces the same problem as my version.
 
Upvote 0
VBA Code:
Selection.AutoFilter Field:=1, Criteria1:=EarningsDay
To use that line the date format has to match the format used in the column exactly.
For example this would work.
VBA Code:
Selection.AutoFilter Field:=1, Criteria1:=Format(EarningsDay, Sheets("Operations").Range("A3").NumberFormat)
Its a high risk approach since I have seen date columns that have been appended to having inconsistent formatting applied.

Since you already have EarningsDay declared as long this will work and would be a safer approach.
VBA Code:
Selection.AutoFilter Field:=1, Criteria1:=">=" & EarningsDay, Operator:=xlAnd, Criteria2:="<" & EarningsDay + 1
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,814
Messages
6,181,128
Members
453,021
Latest member
Justyna P

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