assign Code to Dynamic CommandButton

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
626
Office Version
  1. 2016
Platform
  1. Windows
Hello Every one I have Created

within my Userform9 Code

VBA Code:
Private Sub userform_initialize()

I have created the Following Command Button

VBA Code:
Set CmdBtn = UserForm9.Controls.Add("Forms.CommandButton.1")
    With CmdBtn
        .Caption = "ENTER"
        .Name = "CmdBtn" & 1
        .Left = 350
        .Top = 33
        .Width = 70
    End With

The button is created however I need to run code when "ENTER" button is CLICK

I have tried

VBA Code:
Private Sub CommandButton1_Click()
MsgBox "You Clicked the Command button", vbOKOnly
End Sub

What will I need to add to make it work. any help is greatly appreciated.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
once you gave it a name it was no longer CommandButton1, use the name you gave it
 
Upvote 0
Solution
Why not just add the button to the userform at design time, rather than with code?
 
Upvote 0
Why not just add the button to the userform at design time, rather than with code?
The entire Userform is dynamic so the position of the button is also dynamic.
NoSparks
once you gave it a name it was no longer CommandButton1, use the name you gave it

I thought I tried it with the giving name but must have a different name when I place the Click Sub. but it work when copied the name Thanks. for yall help.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top