assign Code to Dynamic CommandButton

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
623
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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,213,526
Messages
6,114,136
Members
448,551
Latest member
Sienna de Souza

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