excel - vba Range.Autofilter 在 Excel 2011 (Mac) 上失败

标签 excel vba macos

我的代码中有一个公共(public)的关注子,如下所示:

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 中),所以我将引用传递给子,它应该将表重置为未过滤状态。它在带有 Excel 2007 的 PC 上运行良好,但在 Mac 上的 Excel 2011 上失败:

Method 'Autofilter' of Object 'Range' failed



Excel 2011 VBA Reference的以下链接显示 AutoFilter Range 的方法对象,它与我在 Excel 2007 VBA 引用的引用中看到的相匹配。

那么任何人都可以看到为什么这会失败吗?

最佳答案

我无法获得您的 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

关于excel - vba Range.Autofilter 在 Excel 2011 (Mac) 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860328/

相关文章:

excel - 在vba中搜索多个字符串

vba - Excel VBA : Insert N number of Sheets based on cell value

excel - 用于打开文件的Excel VBA宏崩溃而没有任何错误消息

mysql - 将 Mac 上的 MAMP 链接到 MySQL 和 phpMyAdmin 的不同实例

c++ - 是否有某种工具或助手可以将 MFC/C++ 应用程序移植到 OS X/Cocoa?

带有 if、xor 语句的 Excel 求解器

excel - VBA Excel_比较和计数: Compare cells from two sheets and make a count for lateness

excel - Python Openpyxl sheet.dimensions

python - 使用 COM (pywin32) 创建新的 Excel 进程

macos - 在 Mac OS X 上设置永久环境变量