Home > Software design >  VBA, filtering by date, how to stop excel converting date to number?
VBA, filtering by date, how to stop excel converting date to number?

Time:01-11

I am using the following code, however whenever I check the filter, the date is on the numeric value of the date, and not "20/01/2020", Thanks.

filter_date = "20/01/2020"
filter_date = Format(filter_date, "dd/mm/yyyy")

ActiveSheet.Range("$A$1:$L$10").AutoFilter Field:=8, _
Criteria1:=">=" & Format(filter_date, "dd/mm/yyyy"), Operator:=xlAnd

CodePudding user response:

Alternate Answer

The comments under the question have a solution, but I would suggest keeping the Date datatype and go with something like this, which should work:

Option Explicit

Sub Test()

Dim filter_date As Date  ' Declaring the var type as Date

filter_date = #20/1/2020#  ' Use the # sign around a date to identify it as Date

ActiveSheet.Range("$A$1:$L$10").AutoFilter Field:=8, _
Criteria1:=">=" & filter_date, Operator:=xlAnd

End Sub

I think this is easier and shorter, plus you can more easily read your date in code. Using the # around the date is the way to assign dates.

  •  Tags:  
  • Related