VBA filter not correctly filtering dates from date/time data

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
348
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

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).
Good that you worked it out for yourself anyways
 
Upvote 0

Forum statistics

Threads
1,216,118
Messages
6,128,939
Members
449,480
Latest member
yesitisasport

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