Userform VBA option button get selected option

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
704
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi there,
There's no easy way to get selected option of a option button in a Userform VBA besides read one by one is is true or false (checked or not)???
GroupName works only to avoid multiselection in the group? There's no "grupname.value" option.
Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
In Access VBA you can get the value of the Frame the option buttons are in. But from what I've seen from digging around on the internet, Excel VBA isn't set up that way. This code should work for what you want to do though. These are all generic control names, so change those as needed.

Code:
Private Sub CommandButton1_Click()
For Each ctl In UserForm1.Controls
    If TypeName(ctl) = "OptionButton" Then
        If ctl.Value = True Then
            Select Case ctl.Caption
                Case "OptionButton1"
                    MsgBox "do something"
                Case "OptionButton2"
                    MsgBox "do something else"
            End Select
        End If
    End If
Next ctl
End Sub
 
Upvote 0
You can use this to check all Option Buttons in a UserForm.

Code:
Private Sub CommandButton1_Click()
'Modified  8/16/2019  11:41:28 PM  EDT
For Each Control In Me.Controls
    If TypeName(Control) = "OptionButton" And Control.Value = True Then
        MsgBox Control.Caption
    End If
Next
End Sub
 
Upvote 0
Hi,
If you just want to establish the status for a named group of controls (True / False) you could as an idea, create a function

See if following will do what you want

Code:
 Function GroupNameValue(ByVal Group As String, ByVal ControlType As String) As Boolean    
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = ControlType Then
         GroupNameValue = CBool(Ctrl.Object.GroupName = Group And Ctrl.Value)
         If GroupNameValue Then Exit Function
        End If
    Next Ctrl
End Function

You just pass the group name & type of control in the group

e.g.

Code:
 Private Sub CommandButton1_Click()   
  MsgBox GroupNameValue("G1", "OptionButton")
End Sub


Private Sub CommandButton2_Click()
  MsgBox GroupNameValue("G1", "CheckBox")
End Sub


Hope Helpful

Dave
 
Last edited:
Upvote 0
There's no single function that gives the selected option button?
Like: GroupNameValue("MyGroup")=3 (the 3rd option button ou the groupname "MyGroup" is checked (true))
 
Upvote 0
There's no single function that gives the selected option button?
Like: GroupNameValue("MyGroup")=3 (the 3rd option button ou the groupname "MyGroup" is checked (true))


Apparently there is not a single function.



This is an option without reviewing one by one, it only shows you the last one selected.

Code:
Option Explicit
Dim opSel As String


Private Sub CommandButton1_Click()
  MsgBox "Selected buton: " & opSel
End Sub


Private Sub OptionButton1_Click()
  opSel = OptionButton1.GroupName & " " & OptionButton1.Name
End Sub
Private Sub OptionButton2_Click()
  opSel = OptionButton2.GroupName & " " & OptionButton2.Name
End Sub
Private Sub OptionButton3_Click()
  opSel = OptionButton3.GroupName & " " & OptionButton3.Name
End Sub
Private Sub OptionButton4_Click()
  opSel = OptionButton4.GroupName & " " & OptionButton4.Name
End Sub
Private Sub OptionButton5_Click()
  opSel = OptionButton5.GroupName & " " & OptionButton5.Name
End Sub
Private Sub OptionButton6_Click()
  opSel = OptionButton6.GroupName & " " & OptionButton6.Name
End Sub


------------------------------------------------------------------------

Or this other option, shows you true if any optionbutton is selected.

Code:
Private Sub CommandButton2_Click()
  MsgBox OptionButton1 = OptionButton2 = OptionButton3
End Sub
 
Upvote 0
There's no single function that gives the selected option button?
Like: GroupNameValue("MyGroup")=3 (the 3rd option button ou the groupname "MyGroup" is checked (true))

Not as far as I am aware unless another here reading this thread can offer further insight.

All suggestions posted are workarounds & any one may offer something that will go a little way in helping you.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top