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

samerickson89

New Member
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"
 

Domenic

MrExcel MVP
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!
 

samerickson89

New Member
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"
 

Domenic

MrExcel MVP
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".
 

samerickson89

New Member
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.
 

Domenic

MrExcel MVP
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
 

samerickson89

New Member
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"
 

Domenic

MrExcel MVP
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?
 

samerickson89

New Member
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:

Domenic

MrExcel MVP
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?
 

Some videos you may like

This Week's Hot Topics

Top