0

Some time ago I have a sample code to assign the same procedure for multiple buttons.
Something like this:

For Eeach buttton in Form1
If button is clicked
MsgBox button.Caption.

But I can't find this code now.
Googling I cant found egzactly what I need.
I just remember that I need to insert a class module.
Could someone give me a link or a short example.

Alegro
  • 7,534
  • 17
  • 53
  • 74

1 Answers1

2

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

In the Userform-Module:

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
KekuSemau
  • 6,830
  • 4
  • 24
  • 34