VBA Date Filtering Query

NewOrderFac33

Well-known Member
Joined
Sep 26, 2011
Messages
1,283
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Good afternoon,

When I use the following code to filter an Excel database to all rows where the date in the first field is less than or equal to the value in the named range "DeleteOldest"
Code:
DODCriteriaString = "<=" & Format(Sheets("Database").Range("DeleteOldest").value, "yyyy/mm/dd")
ActiveSheet.Range("Database").AutoFilter Field:=1, Criteria1:=DODCriteriaString, Operator:=xlAnd
my procedure filters my database and returns data rows correctly

However, when I use a variation on the above to return rows where the date in the first field is equal to the value in the named range "DeleteNewest"
Code:
DNDCriteriaString = "=" & Format(Sheets("Database").Range("DeleteNewest").value, "yyyy/mm/dd")
ActiveSheet.Range("Database").AutoFilter Field:=1, Criteria1:=DNDCriteriaString, Operator:=xlAnd
no rows are returned (although there are several)

If I change my criteria string to
Code:
DNDCriteriaString = ">=" & Format(Sheets("Database").Range("DeleteNewest").value, "yyyy/mm/dd")
it works fine, and, for the purposes of my dataset, this is also fine, as there are no rows with a date value value greater than "DeleteNewest"

So, it works with >= and <= but not = alone.

Can anyone suggest why this might be?

Thanks in advance

Pete
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I just discovered that if I change the formatting sequence for the date thus:
Code:
DNDCriteriaString = "=" & Format(Sheets("Database").Range("DeleteNewest").value, "dd/mm/yyyy")
that is from "yyyy/mm/dd" to "dd/mm/yyyy", it works with = on its own just fine.

I have NO idea why this might be, but I'm adding it as a comment to my code for future reference before I forget!

Pete
 
Last edited:
Upvote 0
...and for completeness, I tried both options with both formatting sequences:

For <= and >=, you have to use "yyyy/mm/dd" - "dd/mm/yyyy" won't work
For =, you have to use "dd/mm/yyyy" - "yyyy/mm/dd" won't work

Bizarre, but that's VBA for you!

Pete
 
Upvote 0
Glad you sorted it, but as a matter of interest, what happens if you remove the = sign for an exact match.
Code:
DNDCriteriaString = Format(Sheets("Database").Range("DeleteNewest").value, "dd/mm/yyyy")
 
Last edited:
Upvote 0
Hi, Fluff - thanks for taking an interest :-)

DNDCriteriaString = Format(Sheets("Database").Range("DeleteNewest").value, "yyyy/mm/dd") doesn't work
DNDCriteriaString = Format(Sheets("Database").Range("DeleteNewest").value, "dd/mm/yyyy") does.

Cheers

Pete
 
Upvote 0
Just one of the problems with working with Dates in VBA, for those of us who don't use the US date system.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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