[VBA] How to Determine Which OptionButton is Selected in a UserForm

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

  1. Create a UserForm.
  2. Place three OptionButtons and one CommandButton on the form.
  3. Change the (Name) property of the three OptionButtons to ChoiceOption1, ChoiceOption2, and ChoiceOption3. Set their Captions to “Plan A”, “Plan B”, and “Plan C”.
  4. Change the CommandButton’s (Name) to SubmitButton and its Caption to “Submit”.
  5. 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 counter i to access controls named ChoiceOption1, ChoiceOption2, and ChoiceOption3 sequentially.
  • .Value: The Value property of an OptionButton returns True if it is selected and False if 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.

  1. Check the selection state using the .Value property (Selected = True).
  2. Use the Controls collection and a loop to efficiently check multiple buttons.
  3. Use Exit For to 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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次