简体   繁体   English

如何使用 VBA 在 Excel 2016 中获取筛选条件?

[英]How do I get Filter Criteria in Excel 2016 using VBA?

I am working on an Excel 2016 VBA Macro that applies a filter to the headings column.我正在处理将过滤器应用于标题列的 Excel 2016 VBA 宏。 Afterwards, the user applies the filter criteria.之后,用户应用过滤条件。 I would like to be able to in VBA retrieve the filter criteria that the user applied and save it to a string array.我希望能够在 VBA 中检索用户应用的过滤条件并将其保存到字符串数组中。 Is there a way to access the filter criteria?有没有办法访问过滤条件?

I checked this question and pretty much copied the first part of the code, the only thing is you don't get the field that it is applied to which can be problematic.我检查了这个问题并几乎复制了代码的第一部分,唯一的问题是你没有得到它所应用的领域,这可能会有问题。

Dim sht As Worksheet
Set sht = ActiveSheet
With sht.AutoFilter
    With .Filters
        ReDim filtarr(1 To .Count, 1 To 3)
        For f = 1 To .Count
            With .Item(f)
                If .On Then
                    filtarr(f, 1) = .Criteria1
                    Debug.Print .Criteria1
                    If .Operator Then
                        filtarr(f, 2) = .Operator
                        filtarr(f, 3) = .Criteria2
                        Debug.Print .Operator & ", " & .Criteria2
                    End If
                End If
            End With
        Next f
    End With
End With

I'd like to add a bit to the discussion.我想在讨论中补充一点。 I found this (and other excellent sources of help) when investigating how to "return" the filter status.在研究如何“返回”过滤器状态时,我发现了这个(以及其他优秀的帮助来源)。 In my case, I want to DISPLAY the filter status in a cell on a worksheet.就我而言,我想在工作表的单元格中显示过滤器状态。

As I said, this question and many others like it were quite useful.正如我所说,这个问题和许多其他类似的问题都非常有用。 From that, I was able to build the function shown in the code below.由此,我能够构建下面代码中显示的函数。

I pass it the name of the Table for which I want the filter status... thus it's passed in as a RANGE and it then needs to look in the PARENT (sheet) for information.我将我想要过滤器状态的表的名称传递给它...因此它作为 RANGE 传递,然后需要在 PARENT(工作表)中查找信息。 This is because there may be several Tables on the SHEET from which it comes, so I can't just use the SHEET itself to get Autofilter information.这是因为它来自 SHEET 上可能有几个表,所以我不能只使用 SHEET 本身来获取 Autofilter 信息。

This works well, except for one thing: if the active cell on the worksheet is NOT within the table in question, the function will see the number of filters as zero (WholeTable.Parent.Autofilter.Filters.Count in the sample below).这很好用,除了一件事:如果工作表上的活动单元格不在相关表格内,该函数会将过滤器的数量视为零(下面示例中的 WholeTable.Parent.Autofilter.Filters.Count)。 I do not understand why this is, nor how to prevent it.我不明白为什么会这样,也不明白如何预防。 If the active cell IS within the table range, it works perfectly.如果活动单元格在表格范围内,则效果很好。

Any hints would be appreciated!任何提示将不胜感激!

Code:代码:


Public Function AutoFilterCriteria(ByVal WholeTable As Range) As String

On Error Resume Next

If WholeTable.Parent.AutoFilter Is Nothing Then                     ' if no filter is applied
    AutoFilterCriteria = "None"
    On Error GoTo 0
    Exit Function
End If

Dim LongStr As String, FirstOne As Boolean
LongStr = ""
FirstOne = False

Dim iFilt As Integer
For iFilt = 1 To WholeTable.Parent.AutoFilter.Filters.Count         ' loop through each column of the table
    Dim ThisFilt As Filter
    Set ThisFilt = WholeTable.Parent.AutoFilter.Filters(iFilt)      ' look at each filter
    On Error Resume Next
    With ThisFilt
        If .On Then
            If FirstOne Then LongStr = LongStr & " AND "            ' Get column title
            LongStr = LongStr & "[" & WholeTable.Parent.Cells(WholeTable.Row - 1, WholeTable.Column + iFilt - 1).Value & ":"
            On Error GoTo Handle
            If .Operator = xlFilterValues Then                      ' dont really care to enumerate multiples, just show "multiple"
                LongStr = LongStr & "<Multiple>]"
            ElseIf .Operator = 0 Then
                LongStr = LongStr & .Criteria1 & "]"
            ElseIf .Operator = xlAnd Then
                LongStr = LongStr & .Criteria1 & " AND " & .Criteria2 & "]"
            ElseIf .Operator = xlOr Then
                LongStr = LongStr & .Criteria1 & " OR " & .Criteria2 & "]"
            End If
            On Error GoTo 0
            FirstOne = True
        End If
    End With
Next

AutoFilterCriteria = LongStr
On Error GoTo 0
Exit Function

Handle:
AutoFilterCriteria = "! Error !"
On Error GoTo 0

End Function

the code would to be like this.代码将是这样的。 The code of field is cells(1, f).字段代码为cells(1, f)。

Dim sht As Worksheet
Set sht = ActiveSheet
With sht.AutoFilter
    With .Filters
        ReDim filtarr(1 To .Count, 1 To 4) ' change array
        For f = 1 To .Count
            With .Item(f)
                If .On Then
                    filtarr(f, 1) = .Criteria1
                    filtarr(f, 4) = Cells(1, f) 'field
                    Debug.Print .Criteria1, Cells(1, f)
                    If .Operator Then
                        filtarr(f, 2) = .Operator
                        filtarr(f, 3) = .Criteria2

                        Debug.Print .Operator & ", " & .Criteria2
                    End If
                End If
            End With
        Next f
    End With
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM