所以我试图创建一个复选框来隐藏该行中的单元格读取特定值的所有行。
我已为其分配了以下VBA脚本:
Sub Hide_Rows_Containing_Value()
If CheckBox1 = True Then
Dim c As Range
For Each c In Range("C2:C100").Cells
If c.Value = "Closed" Then
c.EntireRow.Hidden = True
End If
Next c
End If
End Sub
分配给它的反向脚本在取消选中框时取消隐藏这些列。
然而,这似乎不起作用,有没有人有任何想法我出错了?
答案 0 :(得分:0)
如果名为"CheckBox1"
的CheckBox放在 Sheet1
上,请在Sheet1模块中使用此代码
Option Explicit
Private Sub CheckBox1_Click()
Dim c As Range
Application.ScreenUpdating = False
With Sheet1
For Each c In .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row).Cells
If IsError(c) Then c.Value2 = "Closed" 'Hides errors as well
c.EntireRow.Hidden = (c.Value = "Closed" And .CheckBox1.Value)
Next
End With
Application.ScreenUpdating = True
End Sub
这将隐藏或取消隐藏基于C
列中包含单词"Closed"
且选中或未选中CheckBox的单元格的行