Excel VBA:当用户窗体中的任何复选框值更改时,如何运行代码?


0

当用户窗体中的任何复选框值更改时,如何运行代码?

我正在尝试使用类模块,但是代码以我不理解的奇怪方式运行。

仅当且仅当_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”语句似乎可以解决该问题,但它似乎正在发生还是很奇怪。



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.