Is there any way to check whether an option button has been checked?

samerickson89

New Member
Joined
Jun 13, 2019
Messages
38
I'm trying to create a form where, if certain fields are blank, it prevents the user from printing I got most of the code to work, but I can't manage to figure out how to check whether an option button has been selected. A lot of resources I've found say to use something along the lines of

Code:
If OptionButton1.Value = True Then
     MsgBox "checked"
Else
     MsgBox "not checked"
End If

But that doesn't work at all. I've also tried

Code:
If OptionButton1 = True Then...

But that gives me "not checked" whether the radio button is selected or not. Then just for the heck of it I tried

Code:
If OptionButton1 = False Then...

Which gives me "checked" whether the radio button is selected or not. I'm at a loss here... is there a simple way to verify that a radio button has been selected? I'm using the option button under "form controls" not "ActiveX controls"
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
For an option button from Form controls, try...

Code:
Worksheets("Sheet1").OptionButtons("Option Button 1").Value = xlOn

Change the sheet name and option button name accordingly.

Hope this helps!
 
Upvote 0
For an option button from Form controls, try...

Code:
Worksheets("Sheet1").OptionButtons("Option Button 1").Value = xlOn

Change the sheet name and option button name accordingly.

Hope this helps!

That gives me a different error. "Run-time error '1004': Unable to get the OptionButtons property of the Worksheet class"
 
Upvote 0
It means that the specified worksheet does not contain the specified option button. If in fact it does contain the option button, make sure that the name is spelled correctly. Note that the format for the name of an option button from the form controls is "Option Button 1", not "OptionButton1".
 
Upvote 0
It means that the specified worksheet does not contain the specified option button. If in fact it does contain the option button, make sure that the name is spelled correctly. Note that the format for the name of an option button from the form controls is "Option Button 1", not "OptionButton1".

That's interesting that it adds spaces in the option button name, even if the actual name doesn't have spaces. Unfortunately I still get the same error when I include the spaces.
 
Upvote 0
When you insert an option button from the Form Controls, the default name is "Option Button n", where n is the next available number in the collection. But you say that the actual name doesn't contain spaces. Did you change its default name? If not, may you've actually inserted an option button from the ActiveX Controls? If so, you would check whether it was selected as follows...

Code:
If Worksheets("Sheet1").OLEObjects("OptionButton1").Object.Value = True Then
 
Upvote 0
When you insert an option button from the Form Controls, the default name is "Option Button n", where n is the next available number in the collection. But you say that the actual name doesn't contain spaces. Did you change its default name? If not, may you've actually inserted an option button from the ActiveX Controls? If so, you would check whether it was selected as follows...

Code:
If Worksheets("Sheet1").OLEObjects("OptionButton1").Object.Value = True Then

I didn't change the default name, and I don't think I used ActiveX Controls. But just in case, I tried changing the code and now the error I get says "Run-time error '438': Object doesn't support this property or method"
 
Upvote 0
Okay, let's try this then...

1) What is the name of the worksheet that contains your option button?

2) Select your option button, and then look at the Name Box located to the left of the formula bar. What is the name displayed in the Name Box?

3) Can you post the exact code you're using?
 
Upvote 0
Okay, let's try this then...

1) What is the name of the worksheet that contains your option button?

2) Select your option button, and then look at the Name Box located to the left of the formula bar. What is the name displayed in the Name Box?

3) Can you post the exact code you're using?

The worksheet is just named "Sheet1" and the option button is named "OptionButton1"

Here's my current code. I've been testing your methods by just looking at one option button instead of all five, and looking for "True" instead of "False", but this is what I'm actually working with.


Code:
If Worksheets("Sheet1").OLEObjects("OptionButton1").Value = False And _
    Worksheets("Sheet1").OLEObjects("OptionButton2").Value = False And _
    Worksheets("Sheet1").OLEObjects("OptionButton3").Value = False And _
    Worksheets("Sheet1").OLEObjects("OptionButton4").Value = False And _
    Worksheets("Sheet1").OLEObjects("OptionButton5").Value = False Then
    MsgBox "Field 'Containment Required' is mandatory"
    Exit Sub
End If
 
Last edited:
Upvote 0
It looks like you have an option button from the ActiveX Controls. The OLEObject is the container holding the option button object, so you'll need to refer to the Object property of the OLEObject, as I did in Post #6 . So it should be...

Code:
[COLOR=#574123]If Worksheets("Sheet1").OLEObjects("OptionButton1").[/COLOR][COLOR=#ff0000]Object[/COLOR][COLOR=#574123].Value = False And _[/COLOR]

Does this help?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
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