VBA filter not correctly filtering dates from date/time data

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
Office Version
  1. 2019
I have a report with dates and times in one column expressed as 2024-04-23T08:43:31+09:30
I have code that extracts the required date and time from this mess:
VBA Code:
For Each cell In rReportTime
    cell = CDate(Left(WorksheetFunction.Substitute(cell.Value, "T", " "), 19))
Next

I also have code that determines a start and end date of a period of interest
VBA Code:
StartReportDate = CDate(Left(PaymentReport.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Value, 10))
EndReportDate = CDate(Left(PaymentReport.ActiveSheet.Range("A2"), 10))
These lines correctly evaluate to 23/04/2024 and 29/04/2024 respectively

I then need to filter the data according to the start and end dates, but the following line of code returns a blank selection. If I manually filter the data, I can select dates, ignoring the time portion. Why can't the code do the same thing?
VBA Code:
ExpReport.ActiveSheet.Range("A1", Cells(LastRow, LastCol)).AutoFilter Field:=3, Criteria1:=">=" & StartReportDate, Operator:=xlAnd, Criteria2:="<=" & EndReportDate
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Have you tried as below:
VBA Code:
StartReportDate = CDbl(CDate(Left(PaymentReport.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Value, 10)))
EndReportDate = CDbl(CDate(Left(PaymentReport.ActiveSheet.Range("A2"), 10)))
 
Upvote 0
Thanks Georgi, that gets me part way there. The filter is now applied from 23/04/2024 until 28/04/2024, despite EndReportDate correctly evaluating to 29/04/2024 and the filter criteria being ="<="
 
Upvote 0
The 2 values you are using are the first 10 characters so it is just the date without the time. I assume you have declared the variables as dates.
You need to use CLng in your filter ie:
Rich (BB code):
ExpReport.ActiveSheet.Range("A1", Cells(LastRow, LastCol)).AutoFilter Field:=3, Criteria1:=">=" & CLng(StartReportDate), Operator:=xlAnd, Criteria2:="<=" & CLng(EndReportDate)
 
Upvote 0
Not sure the time part would cause that but just as a test, you could swap the 'Cdbl' with 'Clng'
 
Upvote 0
When I implemented your first reply I changed StartReportDate and EndReportDate to Double in the variable declaration. Should they be declared as double, long or date?
 
Upvote 0
If you don't need the time then use Long and since you are only using the first 10 characters you are discarding the time.
 
Upvote 0
PS: Does your filter column (column 3) have date and time ?
If it does you are going to need to up the date by +1 and go with < (instead of <=)
ie
Criteria2:="<" & CLng(EndReportDate + 1)
(you can drop the CLng is you have changed the declaration to Long)
 
Upvote 0
Hmm, trying different combinations of variable types and cDbl() or cLng() didn't change the issue.
After some head scratching, I realised my logic was off. 10:38am on the 29th is 45411.44, which is greater than 45411 (the beginning of the 29th), so to capture all times on the 29th I changed the criteria to ="<" EndReportDate +1
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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