Check if Optionbutton in group is selected

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,427
Office Version
  1. 2016
Platform
  1. Windows
If I have a number of Optionbuttons in a group on a userform is there a way I can efficiently check if one has been selected?

I need an error message if none are....
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
If I have a number of Optionbuttons in a group on a userform is there a way I can efficiently check if one has been selected?

I need an error message if none are....
You didn't say what should happen after the error message is display so I assumed the subroutine should be exited (after any clean-up code is executed)... remove the Exit Sub if you don't want to exit the subroutine at that point. Try code structured like this (change the example GroupName of Rick that I used to your actual GroupName)...
Code:
[table="width: 500"]
[tr]
	[td]  Dim Cntrl As Control
  For Each Cntrl In Controls
    If TypeOf Cntrl Is MSForms.OptionButton Then
      If Cntrl.GroupName = "[B][COLOR="#FF0000"]Rick[/COLOR][/B]" And Cntrl.Value Then GoTo OptionButtonSet
    End If
  Next
  MsgBox "No OptionButton selected!", vbCritical
[COLOR="#008000"][B]  '
  '  Any clean-up code goes here
  '
[/B][/COLOR]  [COLOR="#0000FF"]Exit Sub[/COLOR]
OptionButtonSet:
[B][COLOR="#008000"]  '
  ' Code for when one OptionButton is set within the Group
  '
[/COLOR][/B][/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,057
Members
453,524
Latest member
AshJames

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