AutoFilter Working but not on Date

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
177
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm trying to Autofilter my record and works well with Non-Date criteria and on recorded Macro (with Date Criteria)
but not with Date criteria selected from a covered period.
I attached the sample Workbook and my sample code here

What I'm trying to do here is to be able to autofilter and select From and To Date (Covered Period) and Client Remarks (Col D)


VBA Code:
Option Explicit
'
'   using CalendarForm @ https://trevoreyre.com/portfolio/excel-datepicker/
'
Private Sub btnStart_Click()
    Dim StartDate As Date
    StartDate = CalendarForm.GetDate
    txtStart.value = Format(StartDate, "yyyy.mm.dd")
    
End Sub
Private Sub btnEnd_Click()
    Dim EndDate As Date
    EndDate = CalendarForm.GetDate
    txtEnd.value = Format(EndDate, "yyyy.mm.dd")
End Sub

Private Sub btnFILTER_Click()
    Dim LastRow As Long
    LastRow = Worksheets("TEMP").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

'   Working Showing Filtered Data
    Worksheets("TEMP").Range("A2:AS" & LastRow).AutoFilter Field:=4, Criteria1:="BU*"

'   Working But not showing Expected Result
    Worksheets("TEMP").Range("A1:D" & LastRow).AutoFilter Field:=2, Operator:=xlFilterValues, _
    Criteria1:=">=" & txtStart, Operator:=xlAnd, Criteria2:="<=" & txtEnd

'   Code from Macro Recording
    ActiveSheet.Range("$A$1:$D$390").AutoFilter Field:=2, Operator:= _
        xlFilterValues, Criteria2:=Array(1, "1/31/2023", 1, "5/31/2023")
    ActiveSheet.Range("$A$1:$D$390").AutoFilter Field:=4, Criteria1:="BUYER"
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,
Dates and filters can be problematic what works for some, may not work for others

assuming the dates in the range are real dates, see if update to your code below resolves your issue

Rich (BB code):
Worksheets("TEMP").Range("A1:D" & LastRow).AutoFilter Field:=2, Operator:=xlFilterValues, _
    Criteria1:=">=" & CLng(StartDate), Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)

Dave
 
Last edited:
Upvote 0
With your current code I don't think you will have access to the variables StartDate end EndDate in Dave's @dmt32 modification.
If you find it doesn't work for you and you don't want to set them up as Public Variables, try replacing this:
Rich (BB code):
'   Working But not showing Expected Result
    Worksheets("TEMP").Range("A1:D" & LastRow).AutoFilter Field:=2, Operator:=xlFilterValues, _
    Criteria1:=">=" & txtStart, Operator:=xlAnd, Criteria2:="<=" & txtEnd

With this:
VBA Code:
    dtStart = CLng(CDate(Replace(txtStart, ".", "-")))
    dtEnd = CLng(CDate(Replace(txtEnd, ".", "-")))
    Worksheets("TEMP").Range("A1:D" & LastRow).AutoFilter Field:=2, Operator:=xlFilterValues, _
                   Criteria1:=">=" & dtStart, Operator:=xlAnd, Criteria2:="<=" & dtEnd
 
Upvote 0
Solution

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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