当用户窗体中的任何复选框值发生变化时,如何运行代码?
我正在尝试使用类模块,但代码的运行方式很奇怪,我无法理解。
仅当代码中为 _Change() 事件设置了断点时,代码才会识别复选框的值在第一次发生更改时已更改。所有其他情况下,它不会识别任何更改。
这是我在 Userform_Initalize() 中的代码
Private Sub UserForm_Initialize()
Dim ckCollection As New Collection
Dim ctrl As MSForms.Control
Dim obj As clsCheckBoxes
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.CheckBox _
And Not TypeOf ctrl Is MSForms.OptionButton _
And Not TypeOf ctrl Is MSForms.ToggleButton _
And Not ctrl.Name = "ckEditFileDescription" Then 'do not need this particular checkbox in this class module
Set obj = New clsCheckBoxes
Set obj.Control = ctrl
ckCollection.Add obj
End If
Next ctrl
Set obj = Nothing
以下是我的类模块 clsCheckBoxes 中的代码
Private WithEvents xlCheckBoxes As MSForms.CheckBox
Public Property Set Control(cK As MSForms.CheckBox)
Set xlCheckBoxes = cK
End Property
Private Sub xlCheckBoxes_Change()
Call CheckVisibility
'This is the code I want to run when any of the checkboxes change
'but it seems to only recognize the event if the "Call CheckVisibility" is a
'breakpoint, and even then, it will only recognize the first time a checkbox value
'is changed
End Sub
顺便提一下,“如果 TypeOf ctrl 是 MSForms.CheckBox”会返回我的一些选项框和切换按钮,但我不知道为什么。“Add Not”语句似乎可以解决这个问题,但这种情况的发生似乎仍然很奇怪。