0

I am trying to write some code for a VBA userform that has about 100 checkboxes. I was wondering if there is a way that I could have one piece of code that applies to any checkbox or if I have to write 100 seperate functions for checkbox1_click, checkbox2_click, checkbox3_click, etc.

Thanks for any help in advance :)

edit: I realized that it would help to explain exactly what I am trying to do. There will be 100 check boxes and whenever one is clicked I would like to do this:

Call CheckBoxClicked("checkboxname")

4

1 回答 1

2

把它放在一个名为clsCheckBoxHandler的类模块中

Public WithEvents chk As MSForms.CheckBox

Private Sub chk_Click()
    MsgBox chk.Caption & " Clicked!"
End Sub

然后在用户窗体中

Dim chkCollection As Collection


Private Sub UserForm_Initialize()
Dim cCont As Control
Dim chkH As clsCheckBoxHandler

    Set chkCollection = New Collection

    For Each cCont In Me.Controls

        If TypeName(cCont) = "CheckBox" Then

            Set chkH = New clsCheckBoxHandler
            Set chkH.chk = cCont
            chkCollection.Add chkH

        End If

    Next cCont

End Sub

这只是一个简单的复选框处理程序,它具有单击事件,但可以扩展为涵盖多个控件和事件。

于 2013-08-13T14:10:34.757 回答