How to force a user to select an option button?

Rainmanne

Board Regular
Joined
Mar 10, 2016
Messages
134
Office Version
  1. 2019
Platform
  1. Windows
I've got a model with a code that should depend on the selection of one of two option buttons. When a user opens the model, the both option buttons are unselected. I need to make sure that the user selects one of them before the main code is run. So if nothing is selected, then it should display a message box saying something like "Please select an option" and stop the rest of the code from executing.

I guess it should be an IF statement using OptionButton.Value = False as a condition, but I cannot figure out how to do it. It should be something like

VBA Code:
Sub optionbuttonselect()
If ActiveSheet.OptionButtons("Option Button 2").Value = False Then
If ActiveSheet.OptionButtons("Option Button 4").Value = False Then
MsgBox "Check the options"
    End If
    End If
End Sub

But it obviously does not work.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Why not set one of the Optionbuttons as the default instead of making the user always choose?
 
Upvote 0
One way is to use the worksheet event code.

VBA Code:
Private Sub Worksheet_Activate()
    CheckOptions
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    CheckOptions
End Sub

Private Sub CheckOptions()
    If Not (Me.OptionButton2 Or Me.OptionButton4) Then
        MsgBox "Please select an option"
    End If
End Sub
 
Upvote 0
Why not set one of the Optionbuttons as the default instead of making the user always choose?
I need a user to select one of the options. Otherwise, they might forget to change the option from the default.
One way is to use the worksheet event code.

VBA Code:
Private Sub Worksheet_Activate()
    CheckOptions
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    CheckOptions
End Sub

Private Sub CheckOptions()
    If Not (Me.OptionButton2 Or Me.OptionButton4) Then
        MsgBox "Please select an option"
    End If
End Sub
Thanks a lot. Is it possible to integrate it into the larger macro? A user usually hits the button "Start the Model", which initiates the code.
 
Upvote 0
It's possible. Just include a call to the macro CheckOptions in the larger macro. But what are going to do if they ignore the message box and don't select an option? You have no enforcement mechanism.
 
Upvote 0
It's possible. Just include a call to the macro CheckOptions in the larger macro. But what are going to do if they ignore the message box and don't select an option? You have no enforcement mechanism.
Oh, I mean I need to make sure that the code does not run without the selection. So it should be he message and the abort of the code.
 
Upvote 0
I was actually thinking about something like:

If OptionButton1 is checked then
Go ahead
If not check OptionButton2
If OptionButton2 if checked
Go ahead
If not, abort the code and show the message "Select an option"
End If

It looks like a simple structure but I cannot figure out how to do it and if it works.
 
Upvote 0
It looks like a simple structure but I cannot figure out how to do it and if it works.
Try making a function that returns a boolean value that you can use in your main code to check if a selection has been made

Place in STANDARD module


VBA Code:
Function HasBeenSelected() As Boolean
    Dim i As Long
    For i = 1 To 2
        HasBeenSelected = ActiveSheet.OptionButtons("Option Button " & Choose(i, 2, 4)).Value = xlOn
        If HasBeenSelected Then Exit For
    Next i
    If Not HasBeenSelected Then MsgBox "Please Select An Option", 48, "Selection Required"
End Function

To call it, place following as first line of code in your main procedure

VBA Code:
Sub MyProcedure()
    If Not HasBeenSelected Then Exit Sub
  
    'rest of my code
End Sub

Hope Helpful


Dave
 
Upvote 0
Solution
To call it, place following as first line of code in your main procedure

VBA Code:
Sub MyProcedure()
    If Not HasBeenSelected Then Exit Sub
 
    'rest of my code
End Sub
Thanks a lot. I just cannot figure out where to put End If if it's needed. The macro complains about it whereever I put it :-( I tried to put it straight after the statement, and I've got

Rich (BB code):
Compile error: End if without block if

If I put it at the end of the macro it complains at the end of the code.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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