Check if radio button is selected in frames.

papaz87

New Member
Joined
Nov 22, 2017
Messages
12
I tried searching for this but as i dont know the command I can't seem to find the code.
What im trying to do, I think is pretty easy. I have a Userform with two frames with 4 radio buttons in one and 2 radio buttons in another.
The user must select one in both frames when clicking a command button in the userform. When that is done the userform will unload otherwise i want a MsgBox saying that the user need to select a radio button before finishing.

I tried this even though the "value" option doesnt exist for frames....
Code:
 If Frame1.Value And Frame2.Value = True ThenUnload Me
Else: MsgBox "You have to select an option"
End If

I know this doesnt exist for frames.. but what is the option to see if something is selected inside a frame?

Thanks for the help!
 
Awesome!!!! This works with a few tweaks at the bottom.

Code:
Dim x As Longx = 0
    For Each xControl In Frame1.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
    For Each xControl In Frame2.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
If x < 2 Then MsgBox "You must select a option button"
If x > 1 Then Unload Me
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Glad you sorted it out. It's nice to see users who can read code and modify it if needed.
Awesome!!!! This works with a few tweaks at the bottom.

Code:
Dim x As Longx = 0
    For Each xControl In Frame1.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
    For Each xControl In Frame2.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
If x < 2 Then MsgBox "You must select a option button"
If x > 1 Then Unload Me
 
Upvote 0
Last question i promise. I actually also got two textboxes that also need to have something written in them before the Userform gets to close. When tweaking your code i've got this and it works except one thing. It doesn't close when everything is right...

Code:
x = 0    For Each xControl In Frame1.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
    For Each xControl In Frame2.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
If x < 2 And TextBox1.Value = "" Or TextBox4.Value = "" Then
MsgBox "You must select a option button"
If x > 1 And TextBox1.Value = True Or TextBox4.Value = True Then
Unload Me
End If
End If
 
Upvote 0
Try this:
Code:
Private Sub CommandButton2_Click()
Dim x As Long
x = 0
For Each xControl In Frame1.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
    For Each xControl In Frame2.Controls
            If TypeName(xControl) = "OptionButton" Then
                If xControl.Value = True Then x = x + 1
            End If
            
    Next xControl
If x < 2 Then
MsgBox "You have not selected at least 2 option buttons"
ElseIf TextBox1.Value = "" Or TextBox4.Value = "" Then
MsgBox "You must enter some value in both textboxes"
Else
Unload Me
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,123
Messages
6,128,975
Members
449,480
Latest member
yesitisasport

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