Option Buttons to Populate TextBox

sarasotavince

New Member
Joined
May 14, 2011
Messages
24
Hello One and All:

I have 20 options buttons grouped and each has a unique caption. I am trying to populate a textbox on a different userform with the following code:

Dim i As Integer
With ActiveSheet
For i = 1 To .OptionButtons.Count
If .OptionButtons(i).Value = True Then
Enter_All.PrimaryPieceType.Value = .OptionButtons(i).Caption
End If
Next i
End With

Unload Me

The code runs without error, unloads the userform, but does not populate the selected (true) option buttons caption in the textbox PrimaryPieceType on the Enter_All userform. I must be overlooking something.

Any help is much appreciated. Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi sarasotavince,

I'm a little confused, where is your code located, and where are your Option Buttons located.

The code refers to the "With ActiveSheet" which suggests the Option Buttons are ActiveX controls located on a worksheet, but then you "Unload Me" which suggests the Option Buttons may be on a UserForm "Me".
 
Upvote 0
Thanks for a quick reply and I am confused also :). Let me tell you exactly what I am trying to do. I have a userform that lets the user enter data, checks for various errors, then writes the data to multiple worksheets. All that is working fine. On that userform there is a help button that starts another userform--thus allowing the user to choose an entry for a particular text box on the first userform instead of typing it in manually. (I find some users will make an incorrect entry due to not knowing what all the choices are--so I have listed the top 20 choices on this second userform). On the second user form I have 20 optionbuttons, grouped together so that only one selection is permitted. Upon exiting the second userform, the selected optionbutton should populate a text box on the first user form. I can get this to work if I use 20 IF statements but obviously that is inefficient and lends itself to error and also doesn't allow me to gain any significant code-writing knowledge.

So, forgetting the mistakes from my original post, the code should look something like this (I just don't know how to make it work)

Look at 20 optionbuttons on UserForm2 that have been grouped together
When you find the one selected (true)
Take the caption from that optionbutton and place it in textbox 1
on UserForm1

My lastest effort looks like this (but there are problems here also)--

Dim optionBtn As OptionButton
Dim i As Integer
With PrimaryEquipmentHelp1 ('this is UserForm2)
For i = 1 To 20
If optionBtn(i).Value = True Then
Enter_All.PrimaryPieceType.Value ('this is the text box on UserForm1) = optionBtn(i).Caption
End If
Next i
End With

Unload Me ('closes UserForm2)

Thanks Again!
 
Upvote 0
I've gave this a quick test and it seems to work (UserForm2)
Code:
    For i = 1 To 20
        If Me.Controls("OptionButton" & i).Value = True Then
            UserForm1.TextBox1.Value = Me.Controls("OptionButton" & i).Caption
            Unload Me
            Exit For
        End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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
Back
Top