VBA Loop Through Groups of Option Buttons on User Form

richh

Board Regular
Joined
Jun 24, 2007
Messages
111
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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 
Solution

richh

Board Regular
Joined
Jun 24, 2007
Messages
111
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,363
Messages
5,635,836
Members
416,885
Latest member
hs11

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
Top