0

I've got two buttons on my Userform, one drawn in in the userform vba view-object mode, and one that is added with the following code in the sub Userform_Initialize():

Dim Obj As Object
Set Obj = UserForm1.Controls.Add("Forms.CommandButton.1", "commandbuttondone", True)
With Obj
    .Caption = "filled 1n"
    .Left = 550
    .Height = 40
    .Width = 35
    .Top = 5
    MsgBox Obj.name
End With

The first one is responsive when it is clicked. But the one I add/create programmatically, does nothing.

I've tried calling the following codes after clicking the command button commandbuttondone:

1.

Private Sub commandbuttondone_Click()
    MsgBox ("test message")
End Sub

2.

Private Sub commandbuttondone()
    MsgBox ("test message")
End Sub

3.

Sub commandbuttondone_Click()
    MsgBox ("test message")
End Sub

4.

Sub commandbuttondone()
    MsgBox ("test message")
End Sub

But none work. They are all in the "form/module that also houses the userform_initialize sub, as well as the working first button: OKButton_Click()

So I was wondering if there are any suggestions on why it does not display the message box stating "test message"?

a.t.
  • 1,196
  • 1
  • 9
  • 35
  • 3
    Possible duplicate of [Assign on-click VBA function to a dynamically created button on Excel Userform](http://stackoverflow.com/questions/566770/assign-on-click-vba-function-to-a-dynamically-created-button-on-excel-userform) – CallumDA Mar 21 '17 at 17:46
  • @CallumDA I agree it is a duplicate of that post, thank you, should I hence delete this question? – a.t. Mar 21 '17 at 17:48
  • 1
    Maybe this post is better / more elaborate and detailed: http://stackoverflow.com/questions/563972/adding-controls-to-a-frame-in-an-excel-userform-with-vba – Ralph Mar 21 '17 at 18:13
  • 1
    Possible duplicate of [Adding controls to a frame in an Excel userform with VBA](http://stackoverflow.com/questions/563972/adding-controls-to-a-frame-in-an-excel-userform-with-vba) – Ralph Mar 21 '17 at 18:14
  • 1
    Do you really need to create a button by code? Wouldn't it work by using the property ".Visble"? – J. L. Muller Mar 23 '17 at 12:09

0 Answers0