ShowAllData method of Worksheet class failed

后端 未结 7 1444
后悔当初
后悔当初 2020-11-28 10:17

I notice my VBA script doesn\'t work when there\'s an autofilter already on. Any idea why this is?

    wbk.Activate
    Set Criteria = Sheets(\"Sheet1\").Cel         


        
7条回答
  •  粉色の甜心
    2020-11-28 10:50

    AutoFilterMode will be True if engaged, regardless of whether there is actually a filter applied to a specific column or not. When this happens, ActiveSheet.ShowAllData will still run, throwing an error (because there is no actual filtering).

    I had the same issue and got it working with

    If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
      ActiveSheet.ShowAllData
    End If
    

    This seems to prevent ShowAllData from running when there is no actual filter applied but with AutoFilterMode turned on.

    The second catch Or ActiveSheet.FilterMode should catch advanced filters

提交回复
热议问题