Autofilter with column formatted as date

前端 未结 10 1637
遥遥无期
遥遥无期 2020-11-27 21:58

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

相关标签:
10条回答
  • SO this worked for me pretty clean

    ActiveSheet.Range("$A$1:$K$35727").AutoFilter Field:=1, Criteria1:= _
            ">=" & Range("G1"), Operator:=xlAnd, Criteria2:="<=" & Range("H1")
    

    You can try this as well

    Expected output will start date in my G1 cell and end date will be H1 cell.

    0 讨论(0)
  • 2020-11-27 22:14

    you need to convert the format to the american format, like: ">" & Format([datecell], "mm/dd/yyyy") VBA does not understand another format.

    0 讨论(0)
  • 2020-11-27 22:14

    Match your "dd-mm-yyy" to the format of the column, so if you have "16-Aug-16" as your source data formatting then make the filter as "dd-mmm-yy"

    0 讨论(0)
  • 2020-11-27 22:15

    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.

    0 讨论(0)
  • 2020-11-27 22:18

    This syntax works for me:

    .AutoFilter Field:=2, Operator:=xlFilterValues, Criteria2:=Array(2, Format(Now, "yyyy-mm-dd"))
    

    Hint obtained through a macro registration

    0 讨论(0)
  • 2020-11-27 22:20

    here's the occam's razor solution... try putting this in Autoopen for the spreadsheet or if you need to, modify it for the sheet that you wish to affect. it will cause the drop down filters for the date headers to appear as individual dates and not as a date hierarchy.

    ActiveWindow.AutoFilterDateGrouping = False

    0 讨论(0)
提交回复
热议问题