How to add events to dynamically created controls (buttons, listboxes) in Excel VBA userform

前端 未结 1 449
野趣味
野趣味 2021-01-16 08:47

I am trying to build a userform in real time. (in Excel VBA) Just to try-out, I started with 2 labels, a textbox, a combobox and a commandbutton. I list the code below +

相关标签:
1条回答
  • 2021-01-16 09:14

    You'll need to build a class to handle the events on the controls.

    As an example create a new class module called clsMyEvents.
    Add this code to the class:

    Option Explicit
    
    Public WithEvents MyCombo As MSForms.ComboBox
    Public WithEvents MyButton As MSForms.CommandButton
    
    Private Sub MyCombo_Change()
        MsgBox MyCombo.Name & " was changed to value " & MyCombo.Value
    End Sub
    
    Private Sub MyButton_Click()
    
        Dim BtnNum As Long
    
        BtnNum = Replace(MyButton.Name, "MyButton", "")
    
        MsgBox MyButton.Name & " is " & IIf(BtnNum Mod 2 = 0, "even", "odd")
    
    End Sub
    

    Note: When you enter the WithEvents you'll be able to select most events associated with that type of control.

    Next, create a blank UserForm and add this code:

    Option Explicit
    
    Public MyEvents As New Collection
    
    Private Sub UserForm_Initialize()
    
        Dim tmpCtrl As Control
        Dim CmbEvent As clsMyEvents
        Dim x As Long
    
        'Add some dummy data for the combo-boxes.
        Sheet1.Range("A1:A5") = Application.Transpose(Array("Red", "Yellow", "Green", "Blue", "Pink"))
        Sheet1.Range("B1:B5") = Application.Transpose(Array(1, 2, 3, 4, 5))
        Sheet1.Range("C1:C5") = Application.Transpose(Array(5, 4, 3, 2, 1))
    
        For x = 1 To 5
            'Add the control.
            Set tmpCtrl = Me.Controls.Add("Forms.ComboBox.1", "MyCombobox" & x)
            With tmpCtrl
                .Left = 10
                .Width = 80
                .Top = (x * 20) - 18 'You might have to adjust this spacing.  I just made it up.
                .RowSource = "Sheet1!" & Sheet1.Cells(1, x).Resize(5).Address
            End With
    
            'Attach the event.
            Set CmbEvent = New clsMyEvents
            Set CmbEvent.MyCombo = tmpCtrl
            MyEvents.Add CmbEvent
    
        Next x
    
        For x = 1 To 5
            Set tmpCtrl = Me.Controls.Add("Forms.CommandButton.1", "MyButton" & x)
            With tmpCtrl
                .Left = 100
                .Width = 50
                .Height = 20
                .Top = (x * 20) - 18
                .Caption = "Num " & x
            End With
    
            Set CmbEvent = New clsMyEvents
            Set CmbEvent.MyButton = tmpCtrl
            MyEvents.Add CmbEvent
        Next x
    
    End Sub
    

    Edit: I've updated to include code for a command button as well. When you change the value in a combobox it will tell you the name of the control and the value it was changed to. When you click a button it will tell you if the number on it is odd or even.

    0 讨论(0)
提交回复
热议问题