0

I have a custom user form in Excel where the number of option buttons is dynamic, depending on what a user selects from a combo box. I have written a script that will add and format those option buttons with appropriate captions, groupname, etc. However, I would like it so that after those buttons are added dynamically, if a user clicks a button from a group just added, then more code will be executed. I am aware that one can create a custom class so that multiple userform controls are handled with one event handler, but I can't figure out how to get this to work with controls that were added dynamically to a form, after the form initialized. I am hoping this question makes sense. Thanks in advance for any help you can offer.

  • When you are adding the buttons dynamically, you should keep a track of them... (add them to a variable/array as needed). Afterwards should be easy to access them. That, or loop through all controlls `if controll = button then do this` kind of logic. – FAB Mar 19 '19 at 13:19
  • Hello, DarXyde. Thanks for taking the time to respond. I'm not sure I understand what you're saying, or perhaps my original question wasn't clear. Here is some code I have: – Matthew Pelletier Mar 20 '19 at 14:40
  • Sorry-hit enter not realizing that would complete submission of comments. I'm not sure I understand what you're saying, or perhaps my original question wasn't clear. I do know how to loop through controls, but the problem is after I add controls dynamically, then I need an event to trigger the looping through the new controls and do something. So for example, I can add 3 new option buttons dynamically and add them with the same groupname, but I need to make it so clicking any of those new buttons executes certain code. – Matthew Pelletier Mar 20 '19 at 14:46
  • Well, something like this post: [vba-using-withevents-on-userforms](https://stackoverflow.com/questions/1083603/vba-using-withevents-on-userforms/1923457#1923457). – FAB Mar 21 '19 at 09:55

0 Answers0