简体   繁体   English

使用以数组为条件的自动过滤器并搜索包含

[英]Using autofilter with an array as criteria and searching for Contains

I've done a ton of googling and couldn't find any solutions. 我已经进行了大量的谷歌搜索,找不到任何解决方案。 I have a list of 5-10 items in column A, in column BI have around 500k rows (if it was less I could just loop through them). 我在A列中列出了5-10个项目,在BI列中大约有50万行(如果少于的话,我可以遍历它们)。

All the cells in column B have a lot of data, and I want to filter for the ones that contain somewhere within them one of the items in column A. B列中的所有单元格都有大量数据,我想过滤掉其中包含A列其中一项的单元格。

My problem is that since I am using an array as criteria, I can't seem to add wildcards effectively. 我的问题是,由于我使用数组作为条件,因此我似乎无法有效地添加通配符。 Does anyone have a solution? 有没有人有办法解决吗? Here is my current code: 这是我当前的代码:

Dim LastRow As Integer
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "a").End(xlUp).Row
Range("b1").AutoFilter

Dim Cri() As String
ReDim Cri(2 To LastRow)
Dim i As Integer
For i = 2 To LastRow
    Cri(i) = Cells(i, 1).Value
Next

With ActiveSheet
    .Range("b1").AutoFilter Field:=2, Criteria1:=Cri, Operator:=xlFilterValues
End With

I have tried every way I can think of to include wildcards but it does not work. 我已经尽力尝试包括通配符在内的所有方法,但是它不起作用。

I just thought of one possible solution, looping through an if statement that finds the exact amount of criteria, and making 10 or so different possible autofilter codes. 我只是想到了一种可能的解决方案,即遍历if语句以查找确切数量的条件,并制作10种左右可能的自动过滤器代码。 I'm not sure if that would even work but it would not be elegant at all. 我不确定这是否行得通,但是一点也不优雅。

This short sub seems to do what you are asking fairly quickly. 这个简短的子提示似乎可以很快地完成您要问的事情。

Sub collect_and_filter_Bs()
    Dim c As Long, v As Long, vVALs As Variant, vCRITs As Variant, rng As Range

    Application.ScreenUpdating = False

    With ActiveSheet    '<-set this worksheet reference properly
        If .AutoFilterMode Then .AutoFilterMode = False
        If IsEmpty(.Cells(2, 1)) Then Exit Sub
        vCRITs = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)).Value2
        For c = 1 To UBound(vCRITs, 1)
            vCRITs(c, 1) = Chr(42) & vCRITs(c, 1) & Chr(42)
        Next c

        ReDim vVALs(1 To 1)

        With Intersect(.Columns(2), .Cells(1, 1).CurrentRegion)
            For c = 1 To UBound(vCRITs, 1) Step 2
                If c = UBound(vCRITs, 1) Then
                    .AutoFilter Field:=1, Criteria1:=vCRITs(c, 1)
                Else
                    .AutoFilter Field:=1, Criteria1:=vCRITs(c, 1), Operator:=xlOr, Criteria2:=vCRITs(c + 1, 1)
                End If
                With .Offset(1, 0).Resize(.Rows.Count - 1, 1)
                    If CBool(Application.Subtotal(103, .Columns(1))) Then
                        For Each rng In .SpecialCells(xlCellTypeVisible)
                            vVALs(UBound(vVALs)) = rng.Value2
                            ReDim Preserve vVALs(1 To UBound(vVALs) + 1)
                        Next rng
                    End If
                End With
                .AutoFilter Field:=1
            Next c

            ReDim Preserve vVALs(1 To UBound(vVALs) - 1)
            .AutoFilter Field:=1, Criteria1:=(vVALs), Operator:=xlFilterValues
        End With
    End With

    Application.ScreenUpdating = True

End Sub

Tested on 50K rows of random data. 在5万行随机数据上进行了测试。 Leaves the worksheet with column B filtered for each of the values in column A (wildcarded by the program). 使工作表的B列经过A列中的每个值的过滤(程序通配符)。

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

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