VBA autofilter using date variable, keeps appearing backwards.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
813
Office Version
  1. 365
Platform
  1. Windows
I've got this code which is supposed to filter column 13 between two dates:

Code:
ActiveSheet.Range("A2:AI" & LastrowPP).AutoFilter Field:=13, Criteria1:=">=" & DateValue(Newindate), Criteria2:="<=" & DateValue(NewOutDate)

The dates are "dd/mm/yyyy" formatted, and as an example they are "01/09/2019" and "01/12/2019" so a 3 month range.

When the filter is applied, it looks like I'm getting dates between "09/01/2019" and "12/01/2019" - a 3 day range instead.

I've tried playing around with Format(DateValue(Newindate),"dd/mm/yyyy") but it isn't working, I always get the same result. The variables are declared as such:

Code:
Newindate = Format(DateValue(InDate.Caption), "dd/mm/yyyy")
NewOutDate = Format(DateValue(OutDate.Caption), "dd/mm/yyyy")
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Use CLng to convert to a literal number:

Code:
ActiveSheet.Range("A2:AI" & LastrowPP).AutoFilter Field:=13, Criteria1:=">=" & CLng(DateValue(Newindate)), Criteria2:="<=" & CLng(DateValue(NewOutDate))
 
Upvote 0
Rory to the rescue again!

How do I sign you up to my workplace salary? Hahahaha

Thanks bud!
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,226,113
Messages
6,189,046
Members
453,522
Latest member
Seeker2025

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