Dynamically Created Userform

Engineer Joe

Well-known Member
Joined
Jun 10, 2005
Messages
549
Forgive the lack of correct language, this is new territory for me. For reasons I'd rather not articulate, I'm trying to generate a userform on the fly that creates controls based on ranges or sheets in a workbook. I've done that and so I have a userform where I've created a ton of controls in the userform_initialize event.

VBA Code:
 Set btn = Me.Controls.Add("Forms.CommandButton.1", sht.Name, True)

So, the buttons are easy to generate, but I'm not sure how to assign them an action - which I'd like to do dynamically. What I'd love is to have something that no matter what button is pressed, it passes the button name on to another sub or something like that. I just don't see anything in the controls that allows to assign a macro or anything. I can actually generate txt files with the desired code in them for each button press and then call the text files up as strings...i know how to do that, which means I think I really just need a way to assign the buttons a sort of "on action," then run this code...

The other option that I'm not opposed to is some vba that generates userforms that are not dynamic or a way to preserve the userform I'm generating dynamically as something more permanent. e.g., i'm generating a bunch of controls at initialization, so there's no actual private subs that get generated just because a button is created.

tl;dr: I'm looking for a way to create userforms based on input from a spreadsheet, and the userforms need to be able to call macros from button presses... A general sort of catch-all funnel macro that takes action based on the button name regardless of which button is pressed would be great instead of having to copy/paste a billion times.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Anything is possible but you may be making a rod for your own back.

Read this: Programming The VBA Editor VBE Editor.

Can you not have a prepaired Userform and a Listbox instead of many buttons?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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