简体   繁体   English

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

[英]Hide rows in excel workbook excluding cell color - vba macro

I have the macro that will hide the rows with specific cell color, but i need the other way around.我有一个宏可以隐藏具有特定单元格颜色的行,但我需要反过来。 Hide all the rows in entire workbook excluding specific colors.隐藏整个工作簿中不包括特定颜色的所有行。

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

Note: - The <> sign is not working here.注意: - <> 符号在这里不起作用。 - Also is there any way, as i can speedup the execution - 还有什么办法可以加快执行速度

Thanks,谢谢,

OK, i see the problem now.好的,我现在看到问题了。

The object is to check multiple cells in a row, hiding that row if it doesn't have a color.目的是检查一行中的多个单元格,如果该行没有颜色,则隐藏该行。 But if Cell B8 has the target color... what happens if A8 doesn't?但是如果单元格 B8 有目标颜色……如果 A8 没有,会发生什么? A8 triggers the logic and hides the row. A8 触发逻辑并隐藏行。 Whoops.哎呀。

Give this a try.试试这个。 It reads the whole row checking for the color and, once found, stops reading that row and processes the hide logic, and then moves on to the next row:它读取整行检查颜色,一旦找到,就停止读取该行并处理隐藏逻辑,然后移至下一行:

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

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

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