+ Reply to Thread
Results 1 to 5 of 5

Reference Option Button name in a variable in IF Statement

Hybrid View

  1. #1
    Registered User
    Join Date
    03-04-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Reference Option Button name in a variable in IF Statement

    I'm having trouble figuring out how to reference the name of an option button that I'm storing in a string variable so I can check all the option buttons on my sheet with an IF statement in a loop. I'm populating variable 'strOptionName' from an array at the beginning of each loop iteration and performing the following IF statement where "pre01y" is the name of the option button:

    If Sheets("Sheet1").pre01y = True Then
    The above runs fine and if the "pre01y" option button is selected (true) then the code drops into my IF statement. However, if I am populating a variable name such as "strOptionName" with the same value (option name) it does not work like this:

    If Sheets("Sheet1").strOptionName = True Then
    I get an Object doesn't support property or method error using the option button name in a variable. I have tried doing concatenation, brackets, etc. without any luck. Is it possible to do this and if so how?
    Last edited by diane.perry; 03-04-2014 at 08:25 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Reference Option Button name in a variable in IF Statement

    If it's an ActiveX option button then you'll need to use OLEObjects.
    If Sheets("Sheet1").OLEObjects(strOptionName).Object.Value = True Then
    If posting code please use code tags, see here.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,022

    Re: Reference Option Button name in a variable in IF Statement

    Option Explicit
    
    Private Sub optButton1_Click()
    Dim optBut
    Const strOptionName As String = "optButton1"
    
    ' print the sheet name
    Debug.Print Me.Name
    
    ' loop through the shapes in the worksheet
    For Each optBut In Me.Shapes
        ' print the shape name
        Debug.Print optBut.Name
    Next
    
    ' print the name of a specific shape
    Debug.Print Me.Shapes("optButton1").Name
    
    ' print the name of a specific shape using variable
    Debug.Print Me.Shapes(strOptionName).Name
    
     ' print the value
    Debug.Print Me.OLEObjects(strOptionName).Object.Value
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    03-04-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Reference Option Button name in a variable in IF Statement

    Thank you both, that is exactly what I was looking for. Added the reference to OLEObjects and I'm good to go!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,022

    Re: Reference Option Button name in a variable in IF Statement

    You're welcome. Thanks for the rep.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 08-15-2013, 12:25 PM
  2. [SOLVED] Call Form Control Option Button within an If statement
    By SonOfOdin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2012, 02:23 PM
  3. Userform Option Button to Show/Hide Another Option Button on same Userform
    By R_S_6 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-26-2010, 09:44 AM
  4. Replies: 2
    Last Post: 03-27-2007, 08:30 AM
  5. Replies: 0
    Last Post: 02-20-2006, 06:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1