Filtering Of Date Value Not Working

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,562
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm trying to apply a filter to a date column in my database, but the results are empty. Is someone able to suggest a correction to my code?

Code:
Sub StaffOnChange()
    Dim nWSName As String
    Dim wsSS As Worksheet
    Dim changeDate As Date
    
    nWSName = ActiveSheet.Name
    Set wsSS = Worksheets(nWSName)

    changeDate = wsHold.Range("B3")

    With wsSS
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("B3").AutoFilter Field:=1, Criteria1:=changeDate
        .AutoFilterMode = False
    End With
     
    wsSS.Delete
    
End Sub

The value changeDate = 2022-07-22, is a date, and can be accurately reformatted as 22-Jul-22 ("dd-mmm-yy").
The data in column A of worksheet wsSS are dates, represented as serials of that date. They can be accurately reformatted as "dd-mmm-yy".
In my testing, with changedate = 2022-07-22, the filter is not filtering out the row (72) in which A72 = 44764 (2022-07-22).
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Dave, thank you for your suggestion. Changed my code, but unfortunately still no results.
In case it matters, rows 1 and 2 are reserved for headers. After the filter, all rows of data are hidden. There are no visible cells (except the header values where there are values)
 
Upvote 0
Bump

Still perplexed .... any help overcoming this barrier will be greatly appreciated.

Here is a sample of my data....
WSOP_Test16.xlsm
ABCDEF
1
2LDSP
3Vacancy
44474344743E10.2916670.6258
54474444744
64474544745
74474644746E10.2916670.6258
84474744747E10.2916670.6258
94474844748E10.2916670.6258
104474944749E10.2916670.6258
114475044750E10.2916670.6258
124475144751
134475244752
144475344753E10.2916670.6258
154475444754E10.2916670.6258
164475544755E10.2916670.6258
174475644756E10.2916670.6258
184475744757E10.2916670.6258
194475844758
204475944759
214476044760E10.2916670.6258
224476144761E10.2916670.6258
234476244762E10.2916670.6258
244476344763E10.2916670.6258
254476444764E10.2916670.6258
264476544765
274476644766
284476744767E10.2916670.6258
294476844768E10.2916670.6258
304476944769E10.2916670.6258
314477044770E10.2916670.6258
324477144771E10.2916670.6258
334477244772
344477344773
Sheet3


Here is wsHold.Range("B3")
WSOP_Test16.xlsm
ABC
1DateSerial
2On Open2022-07-1244754
3On Change2022-07-1944761
Hold
Cell Formulas
RangeFormula
C2:C3C2=B2


My code ...
Code:
Sub StaffOnChange()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim staffPath As String
    Dim staffFile
    Dim staffOpn As String
    Dim stfRow As Long
    Dim nWSName As String
    Dim wsSS As Worksheet
    Dim changeDate As Date
    
    Set wsHold = Application.ThisWorkbook.Worksheets("Hold")
    
    staffPath = "D:\WSOP 2020\SupportData\"
    staffFile = wsHold.Range("N1")
    staffOpn = staffPath & staffFile
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    cn.ConnectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & staffOpn & ".xlsm;" & _
        "Extended Properties='Excel 12.0 Macro;HDR=YES';"
    cn.Open

    With rs
        .ActiveConnection = cn
        .Source = "SELECT * from [MASTER$]"
        .LockType = adLockReadOnly
        .CursorType = adOpenForwardOnly
        .Open
    End With

    Worksheets.Add
    nWSName = ActiveSheet.Name
    Set wsSS = Worksheets(nWSName)
    Range("A2").CopyFromRecordset rs
    rs.Close
    cn.Close
    Stop
    changeDate = wsHold.Range("C3").Value

    With wsSS
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("A4").AutoFilter Field:=1, Criteria1:=changeDate
        .AutoFilterMode = False
    End With
     
    Application.DisplayAlerts = False
    wsSS.Delete
    Application.DisplayAlerts = True
    
End Sub

Still not revealing any data. All rows of the data are excluded from the filter and the result is an empty worksheet, except for the header values.
 
Upvote 0
Can you try replacing this
VBA Code:
    With wsSS
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("A4").AutoFilter Field:=1, Criteria1:=changeDate
        .AutoFilterMode = False
    End With

With this:

VBA Code:
    With wsSS
        If .FilterMode Then .ShowAllData
        .Range("A3").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, Format(changeDate, "yyyy-mm-dd"))
        .AutoFilterMode = False
    End With
 
Upvote 0
Hi Alex, I'm afraid that didn't work either. I've attached the workbook with data to see if anyone wants to try it on their end
Sheet1 holds the data that was imported from the closed workbook via the ABODB procedure and for which the filter is applied. I've commented out the portion that sets up the connection and transfers the data over.

I tested in the immediate window with the match function, and in trial and error was able to get a match when matching serials, but unable to without error to get matches using a formatted date (22-07-21 or "2022-07-21") even if the data in the column of data was formatted as such. So, if I can't get the filter to work, I know I can use the match function using the serial number to get to me to a useful position.
 
Upvote 0
Match is much more forgiving in terms of Date format. Filter and Find are much less forgiving.
The spreadsheet you have attached has the date column formatted as General. What date format are you using in the real spreadsheet ?
 
Upvote 0
You could give this a try:
Note: I have changed the changeDate dim from a Date to a String

VBA Code:
    Dim changeDate As String
    changeDate = Application.Text(wsHold.Range("C3").Value, wsSS.Range("A4").NumberFormatLocal)

    With wsSS
        If .FilterMode Then .ShowAllData
        .Range("A3").AutoFilter Field:=1, Criteria1:=changeDate
        .AutoFilterMode = False
    End With
 
Upvote 0
Solution
Hi John_w and Alex ... thank you both for your solutions. Both worked and are giving me the results I seek. I'm not quite sure why, other than to accept what Alex stated "Match is much more forgiving in terms of Date format. Filter and Find are much less forgiving." The column being "General" I guess is how it got ported over from the recordsource as the data I provided is raw and unaltered. The ADODB recordset when carried over did not copy any formatting of the original data, which is fine as it's only reference data, and my vba aplication can format the data as it needs it. The source is only temporary to extract values.
 
Upvote 0

Forum statistics

Threads
1,214,395
Messages
6,119,265
Members
448,881
Latest member
Faxgirl

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