In VBA UserForms, there are many situations where you want the user to choose only one item from multiple options (e.g., selecting gender or survey ratings). The OptionButton control is perfect for this purpose.
OptionButtons within the same group have the property that selecting one automatically deselects the others (mutually exclusive).
This article explains a basic and reliable method to determine which OptionButton the user has selected using VBA.
Sample Code: detecting the Selected OptionButton
This macro checks the state of three OptionButtons when the “Submit” button is clicked and displays the Caption (display text) of the selected item in a message box.
UserForm Code
' Process when the "Submit" button (SubmitButton) is clicked
Private Sub SubmitButton_Click()
' == Define variables ==
Dim i As Long
Dim selectedChoice As String
' Set default value (if nothing is selected)
selectedChoice = "None"
' == Loop through OptionButtons 1 to 3 ==
For i = 1 To 3
' Check if selected (True) or not (False) using .Value property
' Dynamically access controls by name ("ChoiceOption1", "ChoiceOption2", etc.)
If Me.Controls("ChoiceOption" & i).Value = True Then
' If selected, get the Caption of that button and exit the loop
selectedChoice = Me.Controls("ChoiceOption" & i).Caption
Exit For
End If
Next i
' == Display the result in a message box ==
MsgBox "Selected Plan: " & selectedChoice, vbInformation, "Result"
End Sub
Preparation Before Execution
- Create a UserForm.
- Place three OptionButtons and one CommandButton on the form.
- Change the
(Name)property of the three OptionButtons toChoiceOption1,ChoiceOption2, andChoiceOption3. Set their Captions to “Plan A”, “Plan B”, and “Plan C”. - Change the CommandButton’s
(Name)toSubmitButtonand its Caption to “Submit”. - Paste the code above into the form’s code module and run the form.
Explanation of the Code
If Me.Controls("ChoiceOption" & i).Value = True Then
This line is the core part that determines which OptionButton is selected.
Me.Controls("ChoiceOption" & i): This uses the loop counterito access controls namedChoiceOption1,ChoiceOption2, andChoiceOption3sequentially..Value: TheValueproperty of an OptionButton returnsTrueif it is selected andFalseif it is not.
The code inside the If block is executed the moment a button with Value = True is found.
selectedChoice = Me.Controls(...).Caption
Once the selected button is found, this line retrieves its .Caption property (the text visible on the screen, e.g., “Plan A”) and stores it in the selectedChoice variable.
Exit For
Since only one OptionButton can be selected in a group, there is no need to continue looping once the selected button is found. Using Exit For to leave the loop immediately prevents unnecessary processing and makes the code more efficient.
Summary
In this article, we covered how to determine which item is selected in a group of OptionButtons on a UserForm.
- Check the selection state using the
.Valueproperty (Selected =True). - Use the
Controlscollection and a loop to efficiently check multiple buttons. - Use
Exit Forto stop the loop as soon as the selection is found.
By applying these basics, you can create interactive tools that branch logic or change data written to a worksheet based on user selection.
