是否可以自动过滤没有标题的数据集/字段?

问题描述 投票:0回答:1

我想自动过滤没有标题的数据集/字段

怀疑有参数选项,但我想我会问

尝试过.header = 否

很健谈,因为我需要 220 个字符 希望我没有违反论坛规则。

excel vba autofilter
1个回答
0
投票

您可以使用VBA循环数据,检查要过滤的列的内容,并逐行决定是否要显示或隐藏该行。

Const FilterCol As Long = 2  ' Set to the column number you want to filter
Const LookFor = "Your long search string"

Dim row As Range
For Each row In ActiveSheet.UsedRange.Rows
    row.EntireRow.Hidden = Not (row.Cells(1, FilterCol) Like "*" & LookFor & "*")
Next row

如果您的搜索应该灵活(不同的列,不同的搜索词),您需要考虑用户界面。一个简单的例程可能如下所示。

Sub filterMyData()   
    Dim filterCol As Long
    Dim LookFor As String
    Application.ScreenUpdating = False
    On Error GoTo filterMyData_Exit  ' Ensure that screen updating is enabled
    
    filterCol = ActiveCell.Column
    LookFor = InputBox("Please enter the search string:", "Filter your data")
    
    Dim row As Range
    For Each row In ActiveSheet.UsedRange.Rows
        row.EntireRow.Hidden = Not (row.Cells(1, filterCol) Like "*" & LookFor & "*")
    Next row

filterMyData_Exit:
    Application.ScreenUpdating = True
End Sub

请注意,该例程禁用屏幕更新以避免闪烁。 您可以将例程分配给快捷方式(例如“Shift+Ctrl+F”)

© www.soinside.com 2019 - 2024. All rights reserved.