有没有办法在 Excel VBA 中同时 运行 自动筛选到多个列?

Is there a way to run Autofilter to more than one column simultaneously in Excel VBA?

我有一个指定为用户输入搜索框的单元格(称为 'UserSearch'),需要能够使用此输入同时过滤多个列。例如,如果用户搜索 'Apple',我需要 VBA 代码来过滤掉该词出现的所有行,即使它出现在另一列中。我目前坚持只能一次过滤掉一列,但此输入也可能出现在另一列中,但该行不会被过滤,因为它可能已被它之前的列过滤掉。

我当前的代码如下:

Sub search()
    With ActiveSheet.Range("$a:$j")
       .AutoFilter Field:=1, Criteria1:="=*" & Range("UserSearch") & "*", Operator:=xlOr
       .AutoFilter Field:=2, Criteria1:="=*" & Range("UserSearch") & "*", Operator:=xlOr
       .AutoFilter Field:=3, Criteria1:="=*" & Range("UserSearch") & "*"
    End With
End Sub

如您所见,我的目标是能够同时对所有 3 个字段进行 运行 自动过滤(基本上将 3 列视为一列),但上面的代码相互矛盾,没有返回任何行.有人知道使用自动过滤器吗?

你不能为此使用 .AutoFilter 但是可以使用一个小的 vba 代码你可以实现你想要的

假设您的工作表如下所示

将此代码粘贴到模块中

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim rngHide As Range
    Dim FoundIt As Long, i As Long, lRow As Long
    Dim SearchString As String

    '~~> Your search string
    SearchString = "Apple"

    '~~> Change this to the relevant sheet
    Set ws = Sheet1

    '~~> Find the last row
    ' 
    lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

    '~~> Loop through 4 to last row to find the search string
    For i = 4 To lRow
        On Error Resume Next
        FoundIt = Application.WorksheetFunction.Match(SearchString, ws.Rows(i), 0)
        On Error GoTo 0

        '~~> Create a range which needs to be hidden
        If FoundIt = 0 Then
            If rngHide Is Nothing Then
                Set rngHide = ws.Rows(i)
            Else
                Set rngHide = Union(rngHide, ws.Rows(i))
            End If
        End If
        FoundIt = 0
    Next i

    '~~> Hide it if applicable
    If Not rngHide Is Nothing Then rngHide.EntireRow.Hidden = True
End Sub

我已经对代码进行了注释,因此您理解它应该没有问题。但如果你这样做了,那就直接问吧。

在行动

这两个宏更基本,但完成与 Sid 的回答相同的任务...

第一个宏循环遍历范围并检查当前行中的前三个单元格中的搜索文本,如果在任何单元格中找到,它将循环到下一行。如果没有单元格包含搜索文本,该行将被隐藏

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") 'Define your worksheet
Dim UserSearch As String: UserSearch = ws.Range("A2").Value 'Assign the range for the user entry, change as needed

    For Each cel In ws.Range("A4", ws.Cells(ws.Rows.Count, 1).End(xlUp)) 'Loop through the range
        'Using (= and Or) test if any of the first three cells in the current row contain the search text
        If cel.Value = UserSearch Or cel.Offset(, 1).Value = UserSearch Or cel.Offset(, 2).Value = UserSearch Then
            'If the search text is found in any of the cells then loop to the next row
        Else
            'If the search text is not in any of the cells then hide the row
            cel.EntireRow.Hidden = True
        End If
    Next cel

第二个宏循环遍历范围并检查当前行的前三个单元格中的搜索文本,如果未找到,该行将被隐藏

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") 'Define your worksheet
Dim UserSearch As String: UserSearch = ws.Range("A2").Value 'Assign the range for the user entry, change the range as needed

    For Each cel In ws.Range("A4", ws.Cells(ws.Rows.Count, 1).End(xlUp)) 'Loop through the range
        'Using (<> and And) test the first three cells in the current row
        If cel.Value <> UserSearch And cel.Offset(, 1).Value <> UserSearch And cel.Offset(, 2).Value <> UserSearch Then
            'If the search text is not found hide the current row
            cel.EntireRow.Hidden = True
        End If
    Next cel