+ Reply to Thread
Results 1 to 14 of 14

Option Buttons and If statement

Hybrid View

  1. #1
    Registered User
    Join Date
    05-04-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    26

    Option Buttons and If statement

    In summery I have 2 sets of 2 option button groups, for example;

    group 1 has:
    Option_Button_A
    and
    Option_Button_B

    Group 2 has:
    Option_Button_C
    and
    Option_Button_D

    Depending on which 2 buttons are selected I would like to run a different code. (4 in total)

    I tried to use a code along the lines of;

    If  Option_Button_A.value = True & Option_Button_C.Value = True Then
    
    [then the specific code]
    
    else If  Option_Button_A.value = True & Option_Button_D.Value = True Then
    
    ECT....


    However this does not seem to be working.

    Could anybody help out??

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Option Buttons and If statement

    Hello Imperial,

    Without seeing the layout of the controls, it is difficult to answer your question accurately. If you post your workbook, I can guarantee the code will be correct.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Option Buttons and If statement

    not sure if
    .value
    is correct

  4. #4
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Option Buttons and If statement

    never mind. its correct

  5. #5
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Option Buttons and If statement

    If OBA.value=true and OBB.value=true then
    ....
    YOur way is correct as long as you list all ways. there should be (4/(4-2)!/2!)=4x3/2=6 ways.

    ab ac ad
    bc bd
    cd

  6. #6
    Registered User
    Join Date
    05-04-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Option Buttons and If statement

    Thanks John the 'and' instead of the '&' did the trick.

    Also its 4 ways, ab and cd are in the same set, so you cant select them both at the same time.

  7. #7
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Option Buttons and If statement

    OK. didn't no. & isn't a procedure or object. it only works when working with a string such as in a message box

  8. #8
    Registered User
    Join Date
    05-04-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Option Buttons and If statement

    Make sense. I got the idea from the strings in msgbox. Thanks again

  9. #9
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Option Buttons and If statement

    Try using the case statement to simplify your code

    Dim Pointer As Integer
    
    Private Sub CommandButton1_Click()
    
    Select Case Pointer
    
    Case 0
    Dietstate = "Dieting"
    Case 1
    Dietstate = "Diet Ruined"
    Case 2
    Dietstate = "Diet Not Ruined"
    Case 10
    Dietstate = "Starving"
    Case 11
    Dietstate = "Fasting"
    Case 12
    Dietstate = "Eating"
    Case 20
    Dietstate = "Dying"
    Case 21
    Dietstate = "Last Legs"
    Case 22
    Dietstate = "Deceased"
    Case Else
    Dietstate = "Dunno"
    End Select
    
    TextBox2.Value = Dietstate
    End Sub
    
    Private Sub OptionButton1_Click()
    UpdatePointer
    End Sub
    Private Sub OptionButton2_Click()
    UpdatePointer
    End Sub
    Private Sub OptionButton3_Click()
    UpdatePointer
    End Sub
    Private Sub OptionButton4_Click()
    UpdatePointer
    End Sub
    
    Private Sub UpdatePointer()
    Pointer = 0
    If OptionButton1 = True Then Pointer = Pointer + 1
    If OptionButton2 = True Then Pointer = Pointer + 2
    If OptionButton3 = True Then Pointer = Pointer + 10
    If OptionButton4 = True Then Pointer = Pointer + 20
    TextBox1.Text = "Pointer Value = " & Pointer
    End Sub
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    05-04-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Option Buttons and If statement

    Interesting point mehmetcik, but would not be suitable for the particular project I am using.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Option Buttons and If statement

    Hello Imperial,,

    This issue could have been resolved faster without all the Q and A if you had posted the workbook.

  12. #12
    Registered User
    Join Date
    05-04-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Option Buttons and If statement

    Hi Leith,

    the only reason why I didn't post my workbook is, I am working on a academic project. It would be against my university regulations to post the whole workbook.

  13. #13
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Option Buttons and If statement

    Did my advice work?

  14. #14
    Registered User
    Join Date
    05-04-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Option Buttons and If statement

    Yup. thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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