An option button in a VBA UserForm (also called a radio button) lets the user select exactly one choice from a group. You add it from the Toolbox, group buttons by placing them inside a Frame control, and read the selected value in code with If OptionButton1.Value = True Then.
What an Option Button Is and Why It Matters
Option buttons enforce single-choice selection. Unlike checkboxes, which allow multiple independent on/off states, option buttons are mutually exclusive within their group. This makes them ideal for choices like payment method, shipping speed, or report format. In a UserForm, the control is named OptionButton1, OptionButton2, and so on by default, though you should rename them to something meaningful like optCreditCard.
The key behavior: when you click one option button, any other option button in the same group automatically turns off. This happens automatically in VBA, but only if the buttons share the same parent container. If you drop them directly on the UserForm, they form one group. If you want two independent groups (for example, one for color and one for size), you must put each group inside its own Frame control.
How to Add and Configure Option Buttons
- Open the VBA editor (Alt+F11), insert a UserForm from the Insert menu.
- From the Toolbox, click the OptionButton icon (a circle with a dot) and draw it on the form.
- In the Properties window (F4), set the
Captionproperty to the visible text, like “Credit Card”. - Set the
Nameproperty to something descriptive, e.g.,optCredit. - To create a group, add a Frame control from the Toolbox, then draw option buttons inside the frame, not on the form.
- Set the
GroupNameproperty if you prefer grouping without frames, but frames are simpler and visually clearer.
To set a default selection, set the Value property of one button to True in the Properties window or in the UserForm’s Initialize event.
Reading the Selected Option in VBA Code
The most common task is checking which option is selected when the user clicks a command button. Here is a complete example. Assume a UserForm named frmOptions with two option buttons inside a frame named fraPayment, and a command button named cmdOK.
Private Sub cmdOK_Click()
Dim selectedPayment As String
' Check each option button's Value property
If optCredit.Value = True Then
selectedPayment = "Credit Card"
ElseIf optPaypal.Value = True Then
selectedPayment = "PayPal"
Else
selectedPayment = "None selected"
End If
MsgBox "You chose: " & selectedPayment
End Sub
Note: only one button in a group can have Value = True, so the order of the If...ElseIf checks does not matter. You can also use a Select Case structure, but the If approach is straightforward.
Grouping Option Buttons Correctly
The single most common mistake is placing option buttons directly on the UserForm when you need multiple groups. All option buttons directly on the form belong to one implicit group. To create separate groups, use one of two methods:
- Frame control: Draw a Frame, then draw option buttons inside it. Each frame creates a separate group. This is the recommended method because it also visually groups the choices for the user.
- GroupName property: Set the same
GroupNamestring for buttons that should belong together. Buttons with different GroupName values act as separate groups even if placed on the form directly. This works but can be confusing to maintain.
For example, if you have options for color (Red, Green, Blue) and size (Small, Medium, Large), you need six option buttons. Place the three color buttons inside one frame, and the three size buttons inside another frame. Without frames, selecting Red would turn off Small, which is wrong.
Common Problems and How to Fix Them
- Buttons not grouping: Check that each group’s buttons are inside a distinct Frame. If they are on the form directly, they are all one group.
- Can’t select a button at runtime: Ensure the UserForm is shown modally with
UserForm1.Show, not just loaded. Also check that the button is not disabled (Enabled = False). - Value resets to False: If you set a default in the Properties window but it doesn’t stick, set it in the
UserForm_Initializeevent:optCredit.Value = True. - Option buttons not visible: They may be behind another control. Use the Format menu in the VBA editor to bring them forward, or check the
ZOrderproperty. - Click event not firing: The
Clickevent of an option button fires when the user clicks it. If you need to react immediately, usePrivate Sub optCredit_Click(). For checking at the end, the command button’s Click event is more reliable.
Option Button vs. Checkbox vs. ListBox
Choosing the right control depends on the number of choices and the interaction model:
- Option button: Use for 2–5 mutually exclusive choices where the user must see all options at once. Example: payment method.
- Checkbox: Use for independent yes/no toggles. Multiple checkboxes can all be checked. Example: optional add-ons.
- ListBox or ComboBox: Use for many choices (more than 5) or when screen space is limited. A ListBox allows single or multi-select; a ComboBox is a dropdown.
If you have more than five options, option buttons become unwieldy. A ListBox with the MultiSelect property set to fmMultiSelectSingle behaves like option buttons but takes less vertical space.
Keyboard Navigation and Accessibility
Option buttons support keyboard navigation automatically. Pressing Tab moves focus between controls. Within a group of option buttons, the arrow keys move between the buttons, and Space selects the focused one. To make this work well, set the TabIndex property of the first button in each group to a logical order. Also set the Accelerator property (e.g., “C” for Credit) so the user can press Alt+C to select that button. This is a small usability win that many developers overlook.
Frequently Asked Questions
How do I get the selected option button’s caption?
Loop through all option buttons in the frame and check which has Value = True. Then read its Caption. For example: For Each ctl In fraPayment.Controls: If TypeName(ctl) = "OptionButton" And ctl.Value = True Then MsgBox ctl.Caption. This avoids hardcoding each button.
Can I use option buttons without a UserForm?
Yes, you can place option buttons directly on a worksheet using the Forms toolbar or ActiveX controls from the Developer tab. The ActiveX option buttons work similarly, but the VBA code is in the worksheet’s code module. The grouping rules are the same: use a Group Box (Forms) or Frame (ActiveX) to create separate groups.
Why do my option buttons all select together even inside frames?
This happens if you copied and pasted a frame with buttons, then pasted it again. The pasted buttons may retain the same GroupName as the originals. Check the GroupName property of each button and ensure they are unique per group. Also verify the buttons are actually inside the frame—drag them if needed.
How do I deselect all option buttons?
You cannot deselect an option button by clicking it again; clicking always selects it. To clear the group programmatically, set each button’s Value to False in a loop. For example: For Each ctl In fraPayment.Controls: If TypeName(ctl) = "OptionButton" Then ctl.Value = False.
Can I change the appearance of an option button?
Yes. The BackColor, ForeColor, and Font properties change basic styling. The Picture property can add an icon. However, the classic radio-button circle shape cannot be replaced with a custom graphic without using a third-party control. For a modern look, consider using a ToggleButton inside a frame, though it behaves differently.
Is there a limit to how many option buttons I can have?
No practical limit within a UserForm, but performance and usability degrade beyond a few dozen. If you need many choices, switch to a ListBox or a multi-column ComboBox. Also, each option button consumes memory, so for hundreds of choices, a ListBox is far more efficient.
Best Practices for Production Code
Before running any macro that reads option buttons, save a copy of your workbook, because macros cannot be undone with Ctrl+Z. Also, always declare your variables with Dim and use meaningful names. When you have many option buttons, avoid writing separate If statements for each; instead, iterate over the frame’s controls as shown above. This makes the code shorter and easier to maintain when you add or remove options. Finally, test the UserForm with the Tab key and arrow keys to ensure the grouping and focus order are logical, since users expect keyboard navigation to work smoothly.
Related
- Computer Skills Test for Data Entry: What It Covers
- Generating Random Numbers in Python: A Complete Guide
- Python String z Order: Sorting and Comparison
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Select, Copy and Paste in Excel: Practical GuideIn Excel VBA, you can select a range, copy it, and paste it elsewhere, but most macros work…
- Excel VBA: Open a Workbook in the Background, HiddenYes. In Excel VBA, open the file with Workbooks.Open, then set each window in the returned workbook’s Windows…
- VBA Range Is Nothing? Meaning, Checks, and Common FixesIn Excel VBA, a Range is Nothing when an object variable does not currently refer to a range…
- Excel VBA MsgBox 3 Buttons: Options, Results, and ExamplesTo show three buttons in an Excel VBA message box, use a built-in button-set constant such as vbYesNoCancel…
- VBA Array Command: Syntax, Examples, and Practical UsesIn VBA, the “array command” most people mean is the Array function: it creates an array of values,…
- Excel VBA: Find the Last Row of Data in a Column EasilyTo find the last populated row in column A with Excel VBA, use Cells(Rows.Count, "A").End(xlUp).Row, with the worksheet…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.