I'm practicing a class module in Excel VBA.
I have a blank userform inserted manually. From now, no controls will be added manually.
As soon as you initialize the userform, it should appear with a single CommandButton on the Top-Left corner of the form.
Now, Click on the button and you will be served another CommandButton right-below the first button.
Clicking on the newly created button should create another button below the previous button. The process should keep going as long as you keep clicking on the newly created CommandButtons.
Problem
I create the first button on the userform when it is initialized and also create a new button below.
After that nothing happens when clicking on the second/newly created button.
Userform Code
Dim A As New Class2 ' Create an object of the Class (where we declared the events).
Private Sub UserForm_Initialize()
' Create and add the button control.
Dim btEx As MSForms.CommandButton
Set btEx = UserForm1.Controls.Add("Forms.CommandButton.1")
With btEx
.Top = 12
.Left = 12
.Width = 72
.Height = 36
.Caption = "Click Me"
End With
Set A.btEvents = btEx
End Sub
Class Module
Public WithEvents btEvents As MSForms.CommandButton
Private Sub btEvents_click()
' Create and add the button control.
Dim btEx As MSForms.CommandButton
Set btEx = UserForm1.Controls.Add("Forms.CommandButton.1")
With btEx
.Top = 30
.Left = 30
.Width = 72
.Height = 36
.Caption = "Click Me"
End With
End Sub
I'm not able to assign the event to the dynamically created CommandButton from the class module.
Am I missing something or is it not possible in the platform?