Forum Discussion

GeorgieAnne's avatar
GeorgieAnne
Iron Contributor
Nov 23, 2025

Add VBA Code to an Option button dynamically

Hello Excelers,

I hope you are doing well. I am back for some more help and here is what I am trying to accomplish:

I have some code that would add x Option Buttons to a User form. x is Unknown but usually less than 15 but maybe more at times.

I need to assign a macro that would return the newly created Option Button's Caption.

 

In a Module I have:

Dim objOptBtn as MSForms.OptionButton

In a For i = 1 to x loop I have:

Set objOptBtn = uFrmChoice.controls.add("Forms.OptionButton.1","objOptBtn" & i,true)

With objOptBtn

         .Caption = i

         .snglTextWidth = Len(objOptBtn.caption)

         .width = snglTextWidth + 18

         .Left = 10

         .Top = TopLevel + 10

         .Width = 400

         .Height = 18

End With

I remember there was an .OnAction "MacroNameYouWantToRunWhenOptionButtonIsClicked" But I do not see it in the Intelisense????

 

How can I assign code to run once the user clicks on a Option Button on the UserForm and return the Caption of that Option button?

I am trying to ask the user to choose ONE option from a set of unknown options.

 

No RepliesBe the first to reply

Resources