WithEvents用于切换按钮VBA

WithEvents用于切换按钮VBA,vba,togglebutton,userform,Vba,Togglebutton,Userform,我是vba新手,需要为游戏制作32个切换按钮。我想利用withevents使切换按钮的代码完全相同。我不确定如何做到这一点,我已经看过类似的问题,但它们没有意义。非常感谢你的帮助 使用WithEvent进程的第一步是将所有必要的对象添加到要保存在内存中以备将来使用的集合中。初始化UserForm时执行此步骤 在名为UF的UserForm中: Option Explicit Dim ToggleControl As ToggleGroup Dim Toggles As Collection P

我是vba新手,需要为游戏制作32个切换按钮。我想利用withevents使切换按钮的代码完全相同。我不确定如何做到这一点,我已经看过类似的问题,但它们没有意义。非常感谢你的帮助

使用WithEvent进程的第一步是将所有必要的对象添加到要保存在内存中以备将来使用的集合中。初始化UserForm时执行此步骤

在名为UF的UserForm中:

Option Explicit

Dim ToggleControl As ToggleGroup
Dim Toggles As Collection

Private Sub UserForm_Initialize()
Dim oEach As Object

    Set Toggles = New Collection
    With UF
        For Each oEach In .Controls                        ' Loop through all the controls on the form
            If TypeName(oEach) Like "ToggleButton" Then    ' Verify that the object is a ToggleButton
                Set ToggleControl = New ToggleGroup        ' Create a new class object to assign the ToggleButton to
                Set ToggleControl.Action = oEach           ' Assign the ToggleButton (oEach) to the desired Class Action (Action)
                Toggles.add ToggleControl                  ' Add the Class to a collection to be preserved in memory
            End If
        Next oEach
    End With
End Sub
接下来,您需要向项目中添加一个类,并将其命名为ToggleGroup

在名为ToggleGroup的类中:

Option Explicit

Public WithEvents Action As MSForms.ToggleButton

Private Sub Action_Click()

    ' Perform Action ...

End Sub
这将在单击对象时为其创建自定义事件。这些自定义事件在本机事件之前执行,这意味着所有本机控件事件仍将工作。

包括?