如果单元格具有特定颜色,我有一个隐藏范围内行的按钮,我需要它来切换,每次点击都会来回隐藏/取消隐藏行。我不太了解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
也可以在每次点击时更改按钮的文字以与“隐藏”“取消隐藏”进行协调?
答案 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)