且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为多个按钮分配通用步骤?

更新时间:2023-02-09 14:32:34

插入一个新的类模块,并将其命名为 clListener (我才想到).
其中的代码:

Insert a new Class Module and name it clListener (that just came to my mind).
Code in there:

Public WithEvents ct As MSForms.CommandButton

Public Sub ct_Click()
    MsgBox ct.Name & " clicked!"
End Sub

在用户窗体模块中:

Private listenerCollection As New Collection

Private Sub UserForm_Initialize()
    Dim ctItem
    Dim listener As clListener

    For Each ctItem In Me.Controls
        If TypeName(ctItem) = "CommandButton" Then
            Set listener = New clListener
            Set listener.ct = ctItem
            listenerCollection.Add listener
        End If
    Next
End Sub