Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofilter with column formatted as date

I am using an AutoFilter with VBA in Excel that works for regular filters, but not a column formatted as date.

I can filter it manually. If I run my code, it filters nothing but when I check the filter and then only click ok (no change being applied to the filter criteria), it filters correctly.

Here is my code:

ws.ListObjects(SheetName).Range.AutoFilter Field:=3, Criteria1 _
        :=">" & CDate([datecell]), Operator:=xlAnd, Criteria2:= _
        "<=" & CDate(WorksheetFunction.EoMonth([datecell], 3))

It seems to be a common problem, but I have not found a solution.

When I run a recorded macro, it does not work either.

like image 860
Spurious Avatar asked Nov 04 '13 11:11

Spurious


4 Answers

Autofilter() works with 'universal' format yyyy-mm-dd, i.e.:

Criteria1:= ">" & Format([MY_DATE],"yyyy-mm-dd")
Criteria2:= "<=" & Format([MY_DATE],"yyyy-mm-dd")

It's better because Excel can't 'understand' it wrong . If you use mm/dd/yyyy or dd/mm/yyyy Excel can fit 02/jan as 01/feb.

see: http://www.oaltd.co.uk/ExcelProgRef/Ch22/ProgRefCh22.htm

The Rules for Working with Excel (International Issue)

  1. When you have to convert numbers and dates to strings for passing to Excel (such as in criteria for AutoFilter or .Formula strings), always explicitly convert the data to a US-formatted string, using Trim(Str(MyNumber)), or the sNumToUS() function shown earlier, for all number and date types. Excel will then use it correctly and convert it to the local number/date formats.

Edit:

We can create an universal Function using Application.International like:

Sub MySub()
    Select Case application.International(xlDateOrder)
        Case Is = 0
            dtFormat = "mm/dd/yyyy"
        Case Is = 1
            dtFormat = "dd/mm/yyyy"
        Case Is = 2
            dtFormat = "yyyy/mm/dd"
        Case Else
            dtFormat = "Error"
    End Select

    Debug.Print (dtFormat)

    ...
    Criteria1:= ">" & Format([MY_DATE],dtFormat)
    Criteria2:= "<=" & Format([MY_DATE],dtFormat)
    ...

End Sub
like image 79
Makah Avatar answered Nov 05 '22 20:11

Makah


Dates can be tricky with Excel VBA AutoFilter. Some find it easier to just loop through the array to be filtered.

Sometimes I have found that one can use the numeric value of the date, especially when dealing with "dates between"

Criteria1:= ">" & CDbl([datecell])
Criteria2:= "<=" & CDbl(WorksheetFunction.EoMonth([datecell], 3))

Note that the above need to be "real dates" and not strings that look like dates. Even a single "string date" will mess things up.

like image 36
Ron Rosenfeld Avatar answered Nov 05 '22 19:11

Ron Rosenfeld


you need to convert the format to the american format, like:

">" & Format([datecell], "mm/dd/yyyy")

VBA does not understand another format.

like image 5
Karlos Henrique Avatar answered Nov 05 '22 19:11

Karlos Henrique


Karlos Henrique, Thanks for suggesting Format([datecell], "mm/dd/yyyy"). It works in my file. My previous code is :

Range("A1:D" & Trim(Str(LstRec))).AutoFilter Field:=2, _
  Criteria1:=">=" & StrtDt, _
  Operator:=xlAnd, _
  Criteria2:="<=" & EndDt

My modified code is :

Range("A1:D" & Trim(Str(LstRec))).AutoFilter Field:=2, _
  Criteria1:=">=" & Format(StrtDt, "mm/dd/yyyy"), _
  Operator:=xlAnd, _
  Criteria2:="<=" & Format(EndDt, "mm/dd/yyyy")

Thanks.

like image 1
pmyk Avatar answered Nov 05 '22 21:11

pmyk