VBA Loop Through Groups of Option Buttons on User Form

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
Excel 2016 Windows 10

I'm trying to loop through all my controls on a user form to ensure they've been completed; I'm confused on how to check to see if one option button has been selected in a group of buttons. Each button is coupled with only one other button per group name, so if either one in the group is selected, that's acceptable. If both buttons aren't selected, then the user needs to go back and choose one.

VBA Code:
Dim errString   As String
    Dim errFlag     As Boolean
    
    errString = ""
    
    With Me
        For Each cntrl In .Controls
            If .Tag = "r" Then
                Select Case TypeName(cntrl) = "OptionButton"
                    'Not sure how to check if one of the buttons in the group have been selected
                Else
                    If Len(cntrl) = 0 Then
                        errFlag = True
                        errString = errString & cntrl.Name & vbNewLine
                    End If
                End Select
            End If
        Next cntrl
        
        If errFlag Then
            MsgBox "The following required fields have been missed:" & vbNewLine & errString, vbCritical, "Please Complete the Form"
            Exit Function
        End if
End With
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi Richh,

Any reason, you are not selecting an option button (default) in a group.

Set value property to true (in Property Window) to any one in a group.

Thanks,
Saurabh
 
Upvote 0
Solution
Hi Richh,

Any reason, you are not selecting an option button (default) in a group.

Set value property to true (in Property Window) to any one in a group.

Thanks,
Saurabh
Makes a lot of sense just to set everything to false and allow the users to choose true if desired. Thanks for the tip!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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