隐藏Excel工作簿中的行,不包括单元格颜色 - vba宏



我有一个宏可以隐藏具有特定单元格颜色的行,但我需要相反的方法。 隐藏整个工作簿中的所有行,但不包括特定颜色。

Sub Hiderows()
Dim r As Range
Application.ScreenUpdating = False
Range("A7").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
For Each r In Selection
If r.Interior.Color = RGB(255, 124, 128) Or r.Interior.Color = RGB(255, 255, 163) Then
r.EntireRow.Hidden = True
End If
Next
Range("A7").Select
Application.ScreenUpdating = True
End Sub

注意: - <>标志在这里不起作用。 - 还有什么办法,因为我可以加快执行速度

谢谢

好的,我现在看到了问题。

目的是检查一行中的多个单元格,如果没有颜色,则隐藏该行。 但是,如果单元格 B8 具有目标颜色...如果 A8 不这样做会怎样? A8 触发逻辑并隐藏行。 哎 呦。

试一试。 它读取整行检查颜色,一旦找到,就停止读取该行并处理隐藏逻辑,然后移动到下一行:

Sub Hiderows()
Dim r As Range
Dim hasColor As Boolean
hasColor = False
Application.ScreenUpdating = False
Range("A7").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
For Each r In Selection.Rows
For Each c In r.Columns
If c.Interior.Color = RGB(255, 124, 128) Or c.Interior.Color = RGB(255, 255, 163) Then
hasColor = True
Exit For
End If
Next
r.EntireRow.Hidden = Not hasColor
hasColor = False
Next
Range("A7").Select
Application.ScreenUpdating = True
End Sub

最新更新