根据颜色隐藏/取消隐藏行按钮

时间:2017-09-08 13:55:53

标签: excel-vba vba excel

如果单元格具有特定颜色,我有一个隐藏范围内行的按钮,我需要它来切换,每次点击都会来回隐藏/取消隐藏行。我不太了解VBA来修复它。

Sub hide_green()
    Dim Rng As Range
    Dim MyCell As Range
    Set Rng = Range("A11:A100")
    For Each MyCell In Rng
        If MyCell.Interior.ColorIndex = 43 Then
            MyCell.EntireRow.Hidden = True
        End If
    Next MyCell
End Sub

也可以在每次点击时更改按钮的文字以与“隐藏”“取消隐藏”进行协调?

3 个答案:

答案 0 :(得分:1)

只需添加一秒,看看它当前是否隐藏。

Sub hide_green()
Dim Rng As Range
Dim MyCell As Range
Set Rng = Range("A11:A100")
For Each MyCell In Rng
    If MyCell.Interior.ColorIndex = 43 Then
        If MyCell.EntireRow.Hidden = True Then
            MyCell.EntireRow.Hidden = False
        Else
            MyCell.EntireRow.Hidden = True
        End If
    End If
Next MyCell
End Sub

答案 1 :(得分:1)

Liss在您的代码上有很好的构建,但是我会用Select Case替换If语句,只是它更干净。

For Each MyCell In Rng
    If MyCell.EntireRow.Hidden=True Then
        MyCell.EntireRow.Hidden=False
    Else
        Select Case MyCell.Interior.ColorIndex
        Case = 43
            MyCell.EntireRow.Hidden = True
        Case <> 43
            MyCell.EntireRow.Hidden = False
        End Select
    End If
Next MyCell

答案 2 :(得分:0)

使用此选项快速标记行的颜色(在另一列上) enter image description here

然后就像往常一样过滤

enter image description here

enter image description here