Error 1004 when trying to get AutoFilter criteria of date filtering

后端 未结 3 697
耶瑟儿~
耶瑟儿~ 2020-12-17 01:10

I\'m working on a piece of VBA code which should save & restore the current AutoFilter state in Excel. I\'ve been using the code here for a looong time without any issue

相关标签:
3条回答
  • 2020-12-17 01:36

    Seems to be a bug, but it only occurs when using the tree view at the bottom of the filter selector.

    If you use the "datefilters" option and select a filter then it will work correctly.

    0 讨论(0)
  • 2020-12-17 01:37

    For those looking for a way to extract date autofilters selected in the tree view, I have a working solution, using the XML data, posted here:
    Get Date Autofilter in Excel VBA

    0 讨论(0)
  • 2020-12-17 01:47

    An alternative way to store the autofilter state (including the treeview selection of date filters) is to use a custom view.

    '[whatever code you want to run before capturing autofilter settings]
    
        wkbExample.CustomViews.Add ViewName:="cvwAutoFilterSettings", RowColSettings:=True
        
        '[whatever code you want to run with either your autofilter or no autofilter]
        
        wkbExample.CustomViews("cvwAutoFilterSettings").Show
        wkbExample.CustomViews("cvwAutoFilterSettings").Delete
        
        '[whatever code you want to run after restoring original autofilter settings]
    

    Original post by Cyious

    However there are three conditions to make it work:

    1. None of the sheets in the workbook should be protected
    2. The fields to which the autofilter was applied should not be cleared/deleted before showing the custom view
    3. You can not have any ListObjects (i.e. ranges "formatted as table") on any sheet in your workbook. If you have at least one ListObject, the CustomViews feature is disabled altogether and any VBA call to it will cause runtime error 1004. You would have to .UnList all of your ListObjects before saving the custom view.

    Bonus: also column width, freeze pane position and range location within the window are stored in the custom view.

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