VBA Date filter not working

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Part of some code is following:
Code:
dataRng.AutoFilter field:=10, Criteria1:=">" & xxxx
I want to test for dates greater than 30 June 2019, however, no matter how I replace xxxx, after the code runs, the filtered values are hidden.

If I then select the filter drop down arrow, check the applied filter and hit ok, then then updates and shows the right results.

I've tried:
Code:
dataRng.AutoFilter field:=10, Criteria1:=">" & #30/06/2019#
dataRng.AutoFilter field:=10, Criteria1:=">" & DateSerial(Year(Date) + IIf(Month(Date) > 6, 1, 0), 6, 30)
But all have the same behaviour, filtered results not returned.

dataRng is a range that currently evaluates to address A19:K56

This however runs fine:
Code:
dataRng.AutoFilter Field:=10, Criteria1:="<" & CDbl(Date + 7)
to show rows where date values are within next 7 days


J19:J56 has blank cells or dates, formatting and hidden chars have been checked for.

Recorded macro returns:
Code:
Sub Macro1()
'
' Macro1 Macro
'
    ActiveSheet.Range("$A$19:$K$56").AutoFilter Field:=10, Criteria1:= _
        ">30/06/2019", Operator:=xlAnd
End Sub
Which filters as expected, however, if I clear the filter and run recorded macro, all results are hidden.

Thoughts/comments please?

Thanks,
Jack
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Thanks Steve, about to head out but will try that. I actually found a 2015 post of yours moaning about dates in VBA to some OP!
 
Upvote 0
Thanks @steve the fish, CLNG seems to have fixed it:
Rich (BB code):
Select Case LCase$(rng.Value)
        Case "due next year": dataRng.AutoFilter Field:=10, Criteria1:=">" & CLng(DateSerial(adjYear, 6, 30))
        Case "due within 7 days": dataRng.AutoFilter Field:=10, Criteria1:="<" & CLng(Date + 7)
    End Select
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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