Option Button Question

stemar

Board Regular
Joined
Mar 16, 2002
Messages
248
I have a form with 10 option buttons in one frame. My OKButton for the form needs to check that one of them has been checked before hiding the form to do stuff with according to which button has been checked.

I can use a line for each:

If OptionLeave then OK = true
IfOptionSick then OK = true
...
if not OK then
nag user
exit sub

or use a single line

if not optionLeave or OptionSick or .... then
nag user
exit sub

It's similar using the results:

If OptionLeave then DoThis
IfOptionSick then DoThat
etc.

Is there a more elegant way of doing it?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
select Case would be another way but it'd be just like the IF statements so my answer would be no... there isn't a 'more' elegant way of doing this altho some people may come up with a solution.
 
Upvote 0
Are they the only checkbox controls in the frame?

This will loop through all the controls within the frame and return either the caption of the selected option or 'No option selected'.


Code:
Option Explicit
 
Private Sub CommandButton1_Click()
Dim ctl As MSForms.Control
Dim boolChk As Boolean
Dim strOption As String
 
    For Each ctl In Frame1.Controls
 
        If TypeOf ctl Is MSForms.OptionButton Then
        
            If ctl.Value Then
                boolChk = True
                strOption = ctl.Caption
            End If

        End If

    Next ctl

    If boolChk Then
        Label1.Caption = "Option selected - " & strOption
    Else
        Label1.Caption = "No option checked"
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,749
Members
452,940
Latest member
rootytrip

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