隐藏出现特定值的所有行的最有效方法

Most efficient way to hide all rows in which a certain value occurs

如果标题超过一行,Excel 自动过滤器将无法正常工作,而且也无法分配给特定的列。所以我想通过 VBA 宏进行过滤。

我有

Sheet2.Range("A1:A40").Find(what:="Software", _
After:=Cells(4, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False _
).EntireRow.Hidden = True

但这只会隐藏第一次出现 "Software" 的行。没有办法为此使用 .find 还是我必须使用循环?

如果你的范围不是很大,你总是可以遍历它,检查一个值,如果找到就隐藏:

Sub test()
Application.ScreenUpdating = False
Dim lastRow As Integer, i As Integer
Dim rng As Range, cel As Range

lastRow = Sheet2.UsedRange.Rows.Count

For i = lastRow To 1 Step -1
  If Cells(i, 1).Value = "Software" or cells(i,1).Value = "software" Then
     Cells(i, 1).EntireRow.Hidden = True
  End If
Next i
Application.ScreenUpdating = True
End Sub

注意:software 部分区分大小写,这就是我使用 Or.

的原因