Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 基于数组的自动筛选,超过3个元素_Excel_Filter_Vba - Fatal编程技术网

Excel 基于数组的自动筛选,超过3个元素

Excel 基于数组的自动筛选,超过3个元素,excel,filter,vba,Excel,Filter,Vba,编辑:我的变量包含通配符。我在网上看到,如果我使用通配符,自动筛选只能包含两个条件。这是真的吗?如果是这样,这可能是我的问题。不幸。 结束编辑 我正在尝试筛选数据,以显示数据是否包含数组中的六项之一。元素是字符串变量。我的工作代码是: With ActiveSheet .Columns("J").AutoFilter Field:=1, Criteria1:=Array(d3, d2), _ Operator:=xlFilterValues End With 这背后的想法是为

编辑:我的变量包含通配符。我在网上看到,如果我使用通配符,自动筛选只能包含两个条件。这是真的吗?如果是这样,这可能是我的问题。不幸。 结束编辑

我正在尝试筛选数据,以显示数据是否包含数组中的六项之一。元素是字符串变量。我的工作代码是:

With ActiveSheet
    .Columns("J").AutoFilter Field:=1, Criteria1:=Array(d3, d2), _
     Operator:=xlFilterValues
End With
这背后的想法是为了实现我的愿望。但是,我实际上需要数组的6个元素,而不仅仅是两个。不幸的是,当我尝试添加所有六个元素时,什么都没有显示

 With ActiveSheet
    .Columns("J").AutoFilter Field:=1, Criteria1:=Array(d3, d2, d1, d21, d11, d31), Operator:=xlFilterValues
End With
我没有得到一个错误或任何东西。只是什么都没出现。有人知道如何解决这个问题吗?
我已经用数组中的两个元素以及字符串的各种组合(d1、d21等)测试了代码,它们都按预期工作,因此问题不在于变量。

AutoFilter仅限于两个带通配符(*或?)的条件

下面的两个版本允许您根据需要指定任意多的通配符


版本1-应用Autoflier并组合可见范围,循环遍历每个通配符



版本2-循环遍历每个单元格,使用InStr()检查通配符是否存在



哇,这真有趣。谢谢你的回复!我从中学到了一些新东西,这是非常理想的,因为我必须自学VBA。我相信我将来会使用Union系列
Option Explicit

Public Sub FilterRows3WildAF()      '(Optional ByVal showAll As Boolean = False)

    Const FILTER_COL = "A"
    Const WILDCARDS = "Name Street Address Number"  'cell starts with these 4 words

    Dim ws As Worksheet, wild As Variant, lr As Long, toShow As Range, itm As Variant

    Set ws = ActiveSheet
    wild = Split(WILDCARDS) 'will search for cells starting with: Name*, then Street*, etc
    Application.ScreenUpdating = False
    ws.Rows.Hidden = False

    With ws.Range(ws.Cells(1), ws.Cells(ws.Rows.Count, FILTER_COL).End(xlUp))
        lr = .Rows.Count
        Set toShow = .Cells(lr + 1, FILTER_COL)
        For Each itm In wild
            .AutoFilter Field:=1, Criteria1:=itm & "*", Operator:=xlFilterValues
            If .SpecialCells(xlCellTypeVisible).Cells.CountLarge > 1 Then
                Set toShow = Union(toShow, .Offset(1).SpecialCells(xlCellTypeVisible))
            End If
        Next
        .AutoFilter
        .Rows.Hidden = True
        toShow.EntireRow.Hidden = False
    End With
    Application.ScreenUpdating = True
End Sub
Public Sub FilterRows3WildInstr()   '(Optional ByVal showAll As Boolean = False)

    Const FILTER_COL = "A"
    Const WILDCARDS = "Name Street Address Number"  'cell starts with these 4 words

    Dim ws As Worksheet, wild As Variant, lr As Long, arr As Variant
    Dim toHide As Range, r As Long, itm As Variant, found As Boolean

    Set ws = ActiveSheet
    wild = Split(WILDCARDS) 'will search for cells starting with: Name*, then Street*, etc
    ws.Rows.Hidden = False

    With ws.Range(ws.Cells(1), ws.Cells(ws.Rows.Count, FILTER_COL).End(xlUp))
        lr = .Rows.Count
        arr = .Value2
        Set toHide = .Cells(lr + 1, FILTER_COL)
        For r = 1 To UBound(arr)
            For Each itm In wild
                found = InStr(1, arr(r, 1), itm) > 0
                If found Then Exit For
            Next
            If Not found Then Set toHide = Union(toHide, .Cells(r, FILTER_COL))
        Next
        toHide.EntireRow.Hidden = True: .Rows(lr + 1).Hidden = False
    End With
End Sub