0

I want to use a date (which is specified in a cell F2 = 01/11/2022) in a filter:

ActiveSheet.ListObjects("QuotationOverview").Range.AutoFilter Field:=5, Operator:= xlFilterValues, Criteria2:=Array(1, "11-2022")

As you can see I now have the date (month), hard coded "11-2022" but if I change that into Range("F2") the sorting is applied on January 2022. The VBA code expects the format mm/dd/YYYY but the format in Excel is dd/mm/YYYY.

ActiveSheet.ListObjects("QuotationOverview").Range.AutoFilter Field:=5, Operator:= xlFilterValues, Criteria2:=Array(1, Range("F2"))

How can I inform VBA that the format is dd/mm/YYYY and not mm/dd/YYYY?

3
  • 11-2022 isn't a date because it lacks a day of month. Dates have no format either in Excel or VBA, they're binary values. Formats apply only when parsing text into a date, or formatting a date into text. When you enter a date into a cell it's actually saved as an OLE Automation Date (OADate) which is essentially a float whose integral part is an offset sinse 1899-12-31 and the fractional part represents the time of day Commented Nov 2, 2022 at 8:46
  • When you select a Mont/Year style for a cell, Excel still saves a full date, eg 2022-10-01 but displays only the month and year. That's what allows date functions to work with that value. In VBA you can construct a date with DateSerial(2022,10,1) and pass it as an argument anywhere you want to use a date. Commented Nov 2, 2022 at 8:49
  • If Criteria2 expects strings though, you still have to specify a full date, ie including the date, using the US date format. This will be translated into a VBA Date value, no matter the user's locale or the cell style of individual cells Commented Nov 2, 2022 at 8:54

1 Answer 1

0

Try using Format to generate your month-year text values:

ActiveSheet.ListObjects("QuotationOverview").Range.AutoFilter Field:=5, Operator:= xlFilterValues, Criteria2:=Array(1, Format(Range("F2"),"mm-yyyy"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.