简体   繁体   English

在 VBA 自动过滤器中使用字符串数组作为条件

[英]Using string array as criteria in VBA autofilter

I've searched other posts and found similar issues but nothing that could help me specifically.我搜索了其他帖子并发现了类似的问题,但没有什么可以特别帮助我。 I'm trying to take an array of strings and use it as a filter criteria.我正在尝试获取一组字符串并将其用作过滤条件。 It's tricky because the array is created by a function and has a variable number of elements and contents.这很棘手,因为数组是由函数创建的,并且具有可变数量的元素和内容。 I need the autofilter to take it and check column E for each one of its elements.我需要自动过滤器来获取它并检查E 列的每个元素。

I've tried it two ways我试过两种方式

1) 1)

With Sheet17

    .Range("E1").AutoFilter Field:=5, Criteria1:=Application.Transpose(arr)

End With

Result: Applies a filter to column E but fails to select any of the options结果:对 E 列应用过滤器但未能选择任何选项

2) 2)

For i = 0 To counter - 1

    With Sheet17

        .Range("E1").AutoFilter Field:=5, Criteria1:=Application.Transpose(arr(i))

    End With

Next

Note: Counter is an integer representing the number of elements in the array.注意: Counter 是一个整数,表示数组中元素的数量。

Result: This one correctly loops through the array but only selects the last option on the filter - presumably because every time it loops back through it starts over and unchecks every other option so by the end only the most recent option remains checked.结果:这个正确地循环遍历数组但只选择过滤器上的最后一个选项 - 大概是因为每次循环返回它都会重新开始并取消选中所有其他选项,因此到最后只有最近的选项保持选中状态。

You do not need to transpose a single element from an array and you cannot put criteria into the 5 th field if you are only referencing column E.您不需要转置数组中的单个元素,并且如果您仅引用列 E,则不能将条件放入第 5字段。

Dim i As Long, arr As Variant
arr = Array(1, 3)

With Sheet17
    'to filter each value in the array one at a time
    For i = 0 To UBound(arr)
        .Columns("E").AutoFilter Field:=1, Criteria1:=arr(i)
    Next i

    'my values were numbers - AutoFilter likes strings in its array
    For i = LBound(arr) To UBound(arr)
        arr(i) = CStr(arr(i))
    Next i

    'to filter all values in the array at once specify xlFilterValues
    .Columns("E").AutoFilter Field:=1, Criteria1:=arr, _
                             Operator:=xlFilterValues
End With

Specify the Operator:=xlFilterValues when passing an array and the Range.AutoFilter Method likes strings as the values in an array.传递数组时指定Operator:=xlFilterValues并且Range.AutoFilter 方法喜欢字符串作为数组中的值。

The Excel documentation for AutoFilter provides some guidance. AutoFilter的 Excel 文档提供了一些指导。 The Operator parameter takes a XlAutoFilterOperator that specifies how Criteria1 is interpreted. Operator参数采用XlAutoFilterOperator来指定Criteria1的解释方式。 In your case, specifying a value of xlFilterValues will cause Criteria1 to be properly interpreted as an array of filter values.在您的情况下,指定xlFilterValues的值将导致Criteria1被正确解释为过滤器值数组。

The following example demonstrates this:以下示例演示了这一点:

Dim arr As Variant
arr = Array("Alpha", "Bravo", "Charlie")

Sheet17.Range("E1").AutoFilter _
    Field:=5, _
    Criteria1:=arr, _
    Operator:=xlFilterValues

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

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