Option Button

ExcelQQ

New Member
Joined
Feb 17, 2009
Messages
15
I create a User Form and i have couple of option buttons in a frame, and one textbox in the user form. I want textbox shows different string based on the selection of option buttons. Part of string is using selected optionButton's caption. Anyone can provide the code? I don't know how to post mine. thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
Something like this?
Code:
Dim optionIndex As Long
Dim preScriptStrings As Variant

preScriptStrings = Array("none", "Selected: ", "Chosen: ", "You pressed: ")

optionIndex = -1 * OptionButton1.Value - 2 * OptionButton2.Value - 3 * OptionButton3.Value

If 0 < optionIndex Then
    TextBox1.Text = preScriptStrings(optionIndex) & Me.Controls("OptionButton" & optionIndex).Caption
Else
    TextBox1.Text = preScriptStrings(optionIndex)
End If
 
Upvote 0

ExcelQQ

New Member
Joined
Feb 17, 2009
Messages
15
Thanks for the code. I have about 10 option buttons so i didn't use the array. This is the code I wrote and couldn't figure why it's not working. Suggestion?

Private Sub UserForm_Initialize()
Dim ctl as MSforms.OptionButton
Dim v As String, path As String

path="c:\....\excel\"

'f_Selection is the name of the frame which contains all the option buttons
For Each ctl In f_Selection.Controls
If ctl.value=True Then
v=ctl.Caption
'TextBox_Dir is the name of the textbox in the userform
TextBox_Dir.Text=path & v & ".xls"
End if
Next ctl

End Sub
 
Upvote 0

ScottR

Well-known Member
Joined
Apr 17, 2008
Messages
545
Because you've set it up on the userform initialization event and you should have it on all of the option button change events.

None of the option buttons will have a true value when the userform initializes.
 
Upvote 0

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
You could do something like
Code:
Private Sub UserForm_Initialize()
    Dim oneOption As msforms.OptionButton
    Dim path As String
    path = "c:\....\excel\"
    For Each oneOption In Frame1.Controls
        oneOption.Tag = path & oneOption.Caption & ".xls"
    Next oneOption
End Sub

Private Sub OptionButton1_Click()
    TextBox1.Text = OptionButton1.Tag
End Sub
...
Private Sub OptionButton10_Click()
    TextBox1.Text = OptionButton10.Tag
End Sub
 
Upvote 0

ExcelQQ

New Member
Joined
Feb 17, 2009
Messages
15
textbox won't show the text though using .tag below, do you know why?

Private Sub OptionButton1_Click()
TextBox1.Text = OptionButton1.Tag
End Sub
 
Upvote 0

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
Are the textbox and optionbutton names correct?
What does the debugging line
Code:
MsgBox "tag Value-" & optionButton1.Tag
show in that place?
 
Upvote 0

ExcelQQ

New Member
Joined
Feb 17, 2009
Messages
15
Dim oneOption As msforms.OptionButton
For Each oneOption In Frame1.Controls
oneOption.Tag = path & oneOption.Caption & ".xls"
Next oneOption

can't find .tag property while I use the code above, did I define something wrong?

no problem in the code below though
Private Sub OptionButton1_Click()
TextBox1.Text = OptionButton1.Tag
End Sub

thanks in advance.
 
Upvote 0

ExcelQQ

New Member
Joined
Feb 17, 2009
Messages
15
it seems to me no "tag" property if we define option button as msforms.OptionButton, anyway to fix that?
 
Upvote 0

Forum statistics

Threads
1,191,485
Messages
5,986,860
Members
440,055
Latest member
CraigTriesHisBest

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top