Harald has attended:
Excel VBA Intro Intermediate course
Excel VBA: Dynamically generated userform. How to add control ma
I generate a dynamic userform with a procedure and add an commandbutton by usind the following code:
Set mycmd = MyForm.designer.Controls.Add("Forms.CommandButton.1")
With mycmd
.Caption = "OK"
.Top = t + 5 ' move button to the bottom
.Left = 30
.Height = 20
.Width = 50 'Lenth of label field for tick box
.Font.Size = 10
.Font.Bold = True 'makes text bold
.Font.Name = "Arial"
.Visible = True
End With
How can I add a private sub in order to get the OK button to execute the below macro like:
Private Sub CommandButton1_Click()
MsgBox "OK button pressed"
End Sub
As I generate the button in my procedure I can not click on it and do a private sub. If I write the private sub in the same module underneath my main code it does not do anything.
Regards,
Harald
RE: Excel VBA: Dynamically generated userform. How to add contro
Hi Harald, thanks for your query. In your procedure, when you've generated the commandbutton on the fly, your next line of code - with the commandbutton still selected, of course, should be:
Selection.OnAction = "MyMacro"
Hope this helps,
Anthony