VBA autofilter using date variable, keeps appearing backwards.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
810
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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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