Userform VBA option button get selected option

eduzs

Active Member
Joined
Jul 6, 2014
Messages
452
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
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,588
Office Version
365, 2019, 2016
Platform
Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,286
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,280
Office Version
2013
Platform
Windows
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:

eduzs

Active Member
Joined
Jul 6, 2014
Messages
452
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))
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,889
Office Version
2007
Platform
Windows
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,280
Office Version
2013
Platform
Windows
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
 

Forum statistics

Threads
1,082,305
Messages
5,364,401
Members
400,800
Latest member
germanpbv

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top