简体   繁体   English

如何在AutoFilter Excel中获取未选择的条件

[英]how to get the unselected criteria in AutoFilter Excel

Using Excel 2007 with VBA We have a table. 将Excel 2007与VBA结合使用我们有一个表格。 The user has filtered column 5 to certain items. 用户已将第5列过滤到某些项目。 I know I can get the currently selected criteria using: 我知道我可以使用以下方法获取当前选定的条件:

'set table
Dim loStats As ListObject
Set loStats = ws.ListObjects("TableStats")

'get filter list
Dim af5 As Variant
af5 = loStats.AutoFilter.Filters(5).Criteria1

'print
Dim x As Integer
For x = LBound(af5) To UBound(af5)
    Debug.Print af5(x)
Next

But what about when the user has cleared the filter from column 5. This doesn't work after the user has cleared the filter in column 5 但是,当用户清除了第5列中的过滤器后,该怎么办?当用户清除了第5列中的过滤器后,这将不起作用

af5 = loStats.AutoFilter.Filters(5).Criteria1

How do I get the list of possible criteria like the user sees in the dropdown box? 如何获得可能的条件列表,如用户在下拉框中看到的?

So, where do I start? 那么,我从哪里开始呢?

Firstly, you falsely assume that the result will be an array. 首先,您错误地认为结果将是一个数组。 If I choose only one value, then LBound(af5) will return a type mismatch error. 如果仅选择一个值,则LBound(af5)将返回类型不匹配错误。 You can correct it with 您可以用

If IsArray(Arr) Then
    For i = LBound(Arr) To UBound(Arr)
        Debug.Print Arr(i)
    Next
Else
    Debug.Print Arr
End If         

Unfortunately, this doesn't solve anything. 不幸的是,这并不能解决任何问题。 Your procedure prints out not the currently filtered items, but the criteria chosen by the user. 您的过程不会打印当前过滤的项目,而是用户选择的标准。 This will be misleading. 这将产生误导。

Consider a table with two columns. 考虑一个有两列的表。 When the user specifies criteria as >3 the result that you will see is >3 . 当用户将条件指定为>3 ,您将看到的结果是>3 What does it actually tell you about the values which are filtered? 关于过滤后的值,它实际上告诉您什么? You don't know how many (if any) were chosen and which ones they are. 您不知道选择了多少(如果有),选择了哪些。 For example, the second column could store {1, 2, 3}, but also {3, 7, 99}. 例如,第二列可以存储{1、2、3},但也可以存储{3、7、99}。

This is the way I would do it: 这就是我要做的方式:

Sub PrintOutFilteredData()
    Dim Tbl As ListObject
    Dim CellCount As Long
    Dim Cell As Range
    Dim Arr() As Variant
    Dim i As Long

    Set Tbl = ActiveSheet.ListObjects("Table1")

    On Error Resume Next
    CellCount = Tbl.DataBodyRange.Columns(2).SpecialCells(xlCellTypeVisible).Cells.Count
    If Err.Number = 1004 Then
        Debug.Print "All data was filtered out"
        Exit Sub
    End If

    For Each Cell In Tbl.DataBodyRange.Columns(2).SpecialCells(xlCellTypeVisible)
        If IsEmpty(Arr(0)) Then
            ReDim Arr(0 To 0)
            Arr(0) = Cell.Value
        Else
            ReDim Preserve Arr(0 To UBound(Arr) + 1)
            Arr(UBound(Arr)) = Cell.Value
        End If
    Next Cell

    For i = LBound(Arr) To UBound(Arr)
        Debug.Print Arr(i)
    Next i
End Sub

The use of On Error Resume Next prevents the procedure from throwing an error when there are no cells left visible, prints out a message and exits the procedure. 当没有可见的单元格时,使用On Error Resume Next可以防止该过程引发错误,并打印一条消息并退出该过程。

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

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