我想使用一个复选框隐藏具有文本“ XXX”和“ YYY”的两行之间的所有行。首先,我要使用此Sub:定义并设置包含文本的单元格的公共范围:
Public Sub Workbook_Open()
Public D1 As Range, D2 As Range
Set D1 = Sheet3.Columns("A").Find(what:="XXX", LookIn:=xlValues, lookat:=xlWhole)
Set D2 = Sheet3.Columns("A").Find(what:="YYY", LookIn:=xlValues, lookat:=xlWhole)
End Sub
然后我像这样设置activeX复选框:
Private Sub Check_Click()
If Check = True Then
Sheet3.Rows(D1.Row + 1 & ":" & D2.Row - 2).Hidden = False
Else:
Sheet3.Rows(D1.Row + 1 & ":" & D2.Row - 2).Hidden = True
End If
End Sub
但是,我不断收到以下错误:必需的对象。我是在声明Ranges错误吗?我怎样才能解决这个问题?谢谢。
答案 0 :(得分:0)
在帖子下方我的评论中,尝试类似(未测试)
Private Sub Check_Click()
Dim D1 As Range, D2 As Range
With Sheet3
Set D1 = .Columns(1).Find(what:="XXX", LookIn:=xlValues, lookat:=xlWhole)
Set D2 = .Columns(1).Find(what:="YYY", LookIn:=xlValues, lookat:=xlWhole)
If D1 Is Nothing Then Exit Sub
If D2 Is Nothing Then Exit Sub
If Check = True Then
.Rows(D1.Row + 1 & ":" & D2.Row - 2).Hidden = False
Else
.Rows(D1.Row + 1 & ":" & D2.Row - 2).Hidden = True
End If
End With
End Sub
我假设以下
Check
的ActiveX复选框Sheet3