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

Excel 根据颜色隐藏/取消隐藏行按钮,excel,vba,Excel,Vba,我有一个按钮,可以隐藏某个范围内的行,如果单元格有特定的颜色,我需要它在每次单击时来回切换隐藏/取消隐藏行。我对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

我有一个按钮,可以隐藏某个范围内的行,如果单元格有特定的颜色,我需要它在每次单击时来回切换隐藏/取消隐藏行。我对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

此外,是否可以更改每次单击按钮的文本以与“隐藏”“取消隐藏”协调?

如果要查看按钮当前是否隐藏,请添加第二个按钮

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

只需添加第二个if,查看它当前是否隐藏

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

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
Rng中每个迈塞尔的

如果MyCell.EntireRow.Hidden=True,则
MyCell.EntireRow.Hidden=False
其他的
选择Case MyCell.Interior.ColorIndex
案例=43
MyCell.EntireRow.Hidden=True
案例43
MyCell.EntireRow.Hidden=False
结束选择
如果结束
下一个迈塞尔

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
Rng中每个迈塞尔的

如果MyCell.EntireRow.Hidden=True,则
MyCell.EntireRow.Hidden=False
其他的
选择Case MyCell.Interior.ColorIndex
案例=43
MyCell.EntireRow.Hidden=True
案例43
MyCell.EntireRow.Hidden=False
结束选择
如果结束
下一个迈塞尔

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

然后像往常一样进行过滤


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

然后像往常一样进行过滤


您可以通过以下方式使用过滤器color@Rosetta我不知道是否存在按颜色过滤的过滤器。我很想知道怎么做。谢谢!你用什么版本的Excel?如果大于等于2007年。您可以不需要任何VBA代码而按颜色过滤,按颜色过滤仅适用于较新版本的Excel。我相信它是在2007年添加的。你可以通过color@Rosetta我不知道是否存在按颜色过滤的过滤器。我很想知道怎么做。谢谢!你用什么版本的Excel?如果大于等于2007年。您可以不需要任何VBA代码而按颜色过滤,按颜色过滤仅适用于较新版本的Excel。我相信它是在2007年添加的。我明白你的意思,尽管我不确定如何仍然使用If-Else语句在第二次单击时取消隐藏行。我明白你的意思,尽管我不确定如何仍然使用If-Else语句在第二次单击时取消隐藏行。