Using Case selection with option buttons / radio buttons

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Using Case selection with option buttons / radio buttons

Post by ABabeNChrist »

I have a userform with 6 six possible selections with the use of Option buttons/Radio buttons. Once making a selection using Option buttons/Radio buttons the user would then select command button located on same userform. The reason for a command button in the first place is because range and font colorindex are the same with each selection. The option buttons are used to add an interior color. I thought of maybe using an If then statement first like so.
not sure how to set up correctly.

Code: Select all

Private Sub CommandButton2_Click()
    Dim OB As Variant

    If OptionButton1.Value = True Then
        OptionButton1.Value = 1
    End If

    If OptionButton2.Value = True Then
        OptionButton2.Value = 2
    End If

    If OptionButton3.Value = True Then
        OptionButton3.Value = 3
    End If

    If OptionButton4.Value = True Then
        OptionButton4.Value = 4
    End If

    If OptionButton5.Value = True Then
        OptionButton5.Value = 5
    End If

    If OptionButton6.Value = True Then
        OptionButton6.Value = 6
    End If

    'OB = OptionButton.Value

    Select Case OB
    Case 1

    Case 2

    Case 3

    Case 4

    Case 5

    Case 6

    End Select

End Sub

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Case selection with option buttons / radio buttons

Post by HansV »

The code makes no sense. The value of an option button is True or False, you cannot set it to a number. But it's not necessary to do it like that. You could use

Code: Select all

Private Sub CommandButton2_Click()
    If OptionButton1.Value = True Then
        Range("A1").Interior.Color = vbRed
    ElseIf OptionButton2.Value = True Then
        Range("A1").Interior.Color = vbGreen
    ElseIf OptionButton3.Value = True Then
        Range("A1").Interior.Color = vbBlue
    ElseIf OptionButton4.Value = True Then
        Range("A1").Interior.Color = vbYellow
    ElseIf OptionButton5.Value = True Then
        Range("A1").Interior.Color = vbCyan
    ElseIf OptionButton6.Value = True Then
        Range("A1").Interior.Color = vbMagenta
    End If
End Sub
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Case selection with option buttons / radio buttons

Post by HansV »

Or, since the value of an option button is either True or False, you could use

Code: Select all

Private Sub CommandButton2_Click()
  Select Case True
    Case OptionButton1.Value
        Range("A1").Interior.Color = vbRed
    Case OptionButton2.Value
        Range("A1").Interior.Color = vbGreen
    Case OptionButton3.Value
        Range("A1").Interior.Color = vbBlue
    Case OptionButton4.Value
        Range("A1").Interior.Color = vbYellow
    Case OptionButton5.Value
        Range("A1").Interior.Color = vbCyan
    Case OptionButton6.Value
        Range("A1").Interior.Color = vbMagenta
  End Select
End Sub
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Case selection with option buttons / radio buttons

Post by ABabeNChrist »

Very cool thank you Hans
I'm kinda new with option buttons
the does and donts