2011-07-28 100 views
1

我有以下在我的代碼子公失敗,如下圖所示:VBA Range.Autofilter上的Excel 2011(蘋果機)

Public Sub ResetFilters(ByRef tbl As ListObject) 
    With tbl 

     '// If Filter Arrows are OFF - turns them on 
     '// If Filter Arrows are ON - turns them off and resets filter 
     .Range.AutoFilter 

     '// Always turns filter arrows to on and sorts table by first field 
     .Range.AutoFilter Field:=1 
    End With 
End Sub 

,你可以看到我使用的Excel表格(ListObjects在VBA說吧)所以我將參考傳遞給sub,並且它應該將表重置爲未過濾狀態。它工作正常,在PC上用Excel 2007中,但在Mac上失敗Excel的2011年:對象的

法「自動篩選」「範圍」失敗

以下鏈接Excel 2011 VBA Reference顯示AutoFilter方法的Range對象,它匹配我在Excel 2007 VBA引用的參考中可以看到的內容。

所以任何人都可以看到爲什麼這是失敗?

回答

0

它似乎並沒有很多人在這個問題上跳躍......無論如何,如果其他人有興趣,我想我找到了一個使用ListObject.ShowAutoFilter屬性的解決方法。它是一個讀/寫布爾屬性,關閉時它將重置ListObject中的過濾器。它還具有在PC上使用Excel 2011 for Mac和Excel 2007(以及2010年)的額外好處。

3

我無法讓ListObject.ShowAutoFilter解決方法爲我工作,特別是因爲我不僅需要關閉自動篩選器,而且還需要在代碼完成後以編程方式恢復篩選器。

我在Mac上做了一些與宏錄製有關的事情,發現即使Range.AutoFilter引發錯誤Selection.AutoFilter沒有。所以我能夠選擇我想要過濾的範圍,然後將我的過濾器應用於選擇。

ws.Range(currentFiltRange).Select 
    Selection.AutoFilter 

如果您需要保存用戶的選擇,你可以很容易地恢復爲好,這裏是我的完整子程序保存自動篩選狀態,運行自己的代碼,然後恢復自動篩選狀態和它的作品上都PC和Mac。

Private Sub saveAndRestoreAutoFilterPCandMAC() 


    Application.ScreenUpdating = False 

    'START SAVING AUTOFILTER STATE 
    Dim ws As Worksheet 
    Dim filterArray() 
    Dim currentFiltRange As String 
    Dim col As Integer 
    Dim usingAutoFilter As Boolean 
    Dim userSelection As String 

    usingAutoFilter = False  
    Set ws = ActiveSheet 

    'Capture AutoFilter settings 
    If ws.AutoFilterMode = True Then 
     With ws.AutoFilter 
      currentFiltRange = .Range.Address 
      If ws.FilterMode = True Then 
       usingAutoFilter = True 
       With .Filters 
        ReDim filterArray(1 To .count, 1 To 3) 
        For col = 1 To .count 
         With .Item(col) 
          If .On Then 
           filterArray(col, 1) = .Criteria1 
           If .Operator Then 
            filterArray(col, 2) = .Operator 
            If .Operator = xlAnd Or .Operator = xlOr Then 
             filterArray(col, 3) = .Criteria2 
            End If 
           End If 
          End If 
         End With 
        Next col 
       End With 
      End If 
     End With 
    End If 
    'END SAVING AUTOFILTER STATE 

    'Remove AutoFilter 
    ws.AutoFilterMode = False 

    'Start Your code here 

    'End of your code 

    'START RESTORE FILTER SETTINGS 
    If Not currentFiltRange = "" Then 
     userSelection = Selection.Address 
     ws.Range(currentFiltRange).Select 
     Selection.AutoFilter 
     If usingAutoFilter Then 
      For col = 1 To UBound(filterArray(), 1) 
       If Not IsEmpty(filterArray(col, 1)) Then 
        If filterArray(col, 2) Then 
         'check if Criteria2 exists and needs to be populated 
         If filterArray(col, 2) = xlAnd Or filterArray(col, 2) = xlOr Then 
          ws.Range(currentFiltRange).Select 
          Selection.AutoFilter Field:=col, _ 
           Criteria1:=filterArray(col, 1), _ 
           Operator:=filterArray(col, 2), _ 
           Criteria2:=filterArray(col, 3) 
         Else 
          ws.Range(currentFiltRange).Select 
          Selection.AutoFilter Field:=col, _ 
           Criteria1:=filterArray(col, 1), _ 
           Operator:=filterArray(col, 2) 
         End If 
        Else 
         ws.Range(currentFiltRange).Select 
         Selection.AutoFilter Field:=col, _ 
         Criteria1:=filterArray(col, 1) 
        End If 
       End If 
      Next col 
     End If 
    End If 
    ws.Range(userSelection).select 

    'END RESTORE FILTER SETTINGS 

    Application.ScreenUpdating = True 

End Sub