Set Click event for command button created with macro

jc113883

Board Regular
Joined
May 29, 2006
Messages
165
Hi all,

I am creating a command button object on a userform with code during run time. What is the best method for specifying the code that will run when the button is clicked?

Graeme.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I don't understand the problem - if you double-click the button on the form in design mode, you can then enter the code that runs 'on click'?
 
Upvote 0
Thanks Andrew. That is a very useful web page... something I've been looking for.

Unfortunately though, that method did not work. The button that I create is created during run time and I want to assign code for it at the same time. Using the CreateEventProc method on the click event fails as the button was not part of the userform when it was loaded and so I get a creat event procedure is invalid error. Furthermore, when I close a userform, any objects I created during run-time disappear.

Another example of where it won't work in run time is as follows. I have a userform with two commandbuttons. The code for the first button creates the Code for the second button. However, this new code for the second button will not work right away. The userform has to be re-loaded.

Further suggestions are appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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