使用excel vba在单个列中自动过滤4个标准

时间:2018-04-18 14:56:50

标签: excel vba excel-vba

i need this kind of filtered data in excel我需要在一列中过滤4个条件。使用过的自动过滤器,但它不起作用。仅适用于2条标准。 The code that I have used and didn't worked

请为此问题提供解决方案。

P.S:我是Excel Vba的初学者。

2 个答案:

答案 0 :(得分:1)

AutoFilter限制为2个标准,包含通配符(*或?)

以下2个版本允许您根据需要指定任意数量的通配符

版本1 - 通过应用AutoFliter并组合可见范围的每个通配符循环

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

版本2 - 循环遍历每个单元格,如果通配符存在则使用InStr()进行检查

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

答案 1 :(得分:0)

重新分发您的数据更容易,但要使用AutoFIlter,您可以通过应用"帮助"将 4 标准降低到 1 标准。列。

辅助列将包含公式:

=IF(OR(ISNUMBER(FIND("Number",A2)),ISNUMBER(FIND("Name",A2)),ISNUMBER(FIND("Street",A2)),ISNUMBER(FIND("Address",A2))),"Display","Hide")

然后我们可以过滤"帮助"。之前:

enter image description here

代码:

Sub User()
    Dim N As Long, r1 As Range, r2 As Range
    Dim s As String

    N = Cells(Rows.Count, "A").End(xlUp).Row
    Range("B1").Value = "helper"
    Set r1 = Range("B2:B" & N)
    s = "=IF(OR(ISNUMBER(FIND(""Number"",A2)),ISNUMBER(FIND(""Name"",A2)),ISNUMBER(FIND(""Street"",A2)),ISNUMBER(FIND(""Address"",A2))),""Display"",""Hide"")"
    r1.Formula = s

    With Range("A:B")
        .AutoFilter
        .AutoFilter Field:=2, Criteria1:="Display"
    End With
End Sub

之后:

enter image description here