Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Is there any way to check whether an option button has been checked?
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jun 2019
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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"

  2. #2
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,732
    Post Thanks / Like
    Mentioned
    29 Post(s)
    Tagged
    5 Thread(s)

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

    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!

  3. #3
    New Member
    Join Date
    Jun 2019
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by Domenic View Post
    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"

  4. #4
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,732
    Post Thanks / Like
    Mentioned
    29 Post(s)
    Tagged
    5 Thread(s)

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

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

  5. #5
    New Member
    Join Date
    Jun 2019
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by Domenic View Post
    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.

  6. #6
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,732
    Post Thanks / Like
    Mentioned
    29 Post(s)
    Tagged
    5 Thread(s)

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

    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

  7. #7
    New Member
    Join Date
    Jun 2019
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by Domenic View Post
    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"

  8. #8
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,732
    Post Thanks / Like
    Mentioned
    29 Post(s)
    Tagged
    5 Thread(s)

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

    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?

  9. #9
    New Member
    Join Date
    Jun 2019
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by Domenic View Post
    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 by samerickson89; Jun 25th, 2019 at 02:12 PM.

  10. #10
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,732
    Post Thanks / Like
    Mentioned
    29 Post(s)
    Tagged
    5 Thread(s)

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

    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:
    If Worksheets("Sheet1").OLEObjects("OptionButton1").Object.Value = False And _
    Does this help?

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •