Excel VBA Saving autofilter settings with dates

后端 未结 3 1701
鱼传尺愫
鱼传尺愫 2021-01-24 01:34

I have a part of a macro that stores the autofilter settings before clearing the filter, so it can reapply them later. The code is as follows:

Dim FilterArray()
         


        
相关标签:
3条回答
  • 2021-01-24 01:50

    I've amended your code and it runs now well also with dates in my Excel 2013:

    ' .Criteria1/2 are read as strings with local decimal separator
    ' but for re-setting Criteria1/2 should be in American format ...
    ' so replace local decimal separator by dot - this seems to apply also for dates, 
    ' because dates are also transformed internally to decimal numbers
    FilterArray(f, 1) = Replace(.Criteria1, Application.International(xlDecimalSeparator), ".")
    

    (similar for .Criteria2)

    Regarding your secondary question: see code example in this post: In Excel VBA, how do I save / restore a user-defined filter?

    0 讨论(0)
  • 2021-01-24 02:01

    I found out, to enable filter Date in Array.

    Below setting must be disable (Enable by default)

    ActiveWindow.AutoFilterDateGrouping = False
    
    0 讨论(0)
  • 2021-01-24 02:14

    Note, this solution below is true of Excel 2010, I cannot find information on Excel 2013, but it is possible the same problem exists.
    If you record a macro while running an auto date filter, you will notice the values are stored in the Criteria2 parameter, and Criteria1 is not used:

    Range.AutoFilter Field:=2, Operator:=xlFilterValues, Criteria2:=Array(2, "8/10/2015", 2, "8/20/2015")
    

    Trying to access the Criteria2 parameter from VBA causes an "Application-defined or object-defined error".
    At the time of writing this answer, the only way I know of saving this autofilter information is by reading the XML data inside the xlsx file. Working code here:
    Get Date Autofilter in Excel VBA

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