繁体   English   中英

有没有办法在 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$4:$j$30")
       .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
    ' https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba
    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

暂无
暂无

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

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