UserForm OptionButton Click

alselten

New Member
Joined
Jan 1, 2015
Messages
3
Hi,

I'm working on a userform that creates option buttons from a range.
Now I would like to set a variable, base on the caption, when clicking a option button.

I tried many things, but noting seems to work for me.
Can anyone help me please?



My code for creating the option buttons:

Private Sub UserForm_Activate()

Cust_Edit.Visible = False
Dim SearchString As String
Dim newbutton As msforms.OptionButton

SearchString = Sheets("Invullijst").Range("I3").Value
Last_Address_Row = Worksheets("Adressen").Range("A65536").End(xlUp).Row

i = 0
For Each sCell In Worksheets("Adressen").Range("A1:A" & Last_Address_Row).Cells
If sCell Like "*" & UCase(SearchString) & "*" Or Cell Like "*" & LCase(SearchString) & "*" Or sCell Like "*" & StrConv(SearchString, vbProperCase) & "*" Then

i = i + 1

Set newbutton = Me.Controls.Add("Forms.OptionButton.1")
With newbutton
.Name = "Opt" & i
.Top = 10 + (20 * i)
.Left = 10
.Width = 200
.Caption = sCell.Value
.GroupName = "Custs"
End With
End If
Next sCell

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
If you REM this line:

<font face=Courier New><SPAN style="color:#007F00">'   Delete the form</SPAN><br><SPAN style="color:#007F00">'    ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm</SPAN><br>    </FONT>

...in Mr. Walkenbach's code (in GetOption()), you can see the event handler code written to the created form's module:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>  GETOPTION_RET_VAL = <SPAN style="color:#00007F">False</SPAN><br>  Unload Me<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> CommandButton2_Click()<br>  <SPAN style="color:#00007F">Dim</SPAN> ctl<br>  GETOPTION_RET_VAL = <SPAN style="color:#00007F">False</SPAN><br>  <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ctl <SPAN style="color:#00007F">In</SPAN> Me.Controls<br>    <SPAN style="color:#00007F">If</SPAN> ctl.Tag <> "" <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">If</SPAN> ctl <SPAN style="color:#00007F">Then</SPAN> GETOPTION_RET_VAL = ctl.Tag<br>  <SPAN style="color:#00007F">Next</SPAN> ctl<br>  Unload Me<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

If you do need to update a variable upon choosing an option (rather than upon the command button), here is an example:

In a new blank workbook...

A userform (I named: frmTest) with one commandbutton default named.
A Class module default named.

In the Class Module:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Private</SPAN> objForm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Declare the form as generic Object</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">WithEvents</SPAN> objOptBttn <SPAN style="color:#00007F">As</SPAN> MSForms.OptionButton<br><SPAN style="color:#00007F">Private</SPAN> strBttnTag <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>  <br><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Set</SPAN> ParentForm(pf <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>)<br>  <SPAN style="color:#00007F">Set</SPAN> objForm = pf<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br>  <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Get</SPAN> ParentForm() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> ParentForm = objForm<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Set</SPAN> OptBttn(ob <SPAN style="color:#00007F">As</SPAN> MSForms.OptionButton)<br>  <SPAN style="color:#00007F">Set</SPAN> objOptBttn = ob<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br>  <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Get</SPAN> OptBttn() <SPAN style="color:#00007F">As</SPAN> MSForms.OptionButton<br>    <SPAN style="color:#00007F">Set</SPAN> OptBttn = objOptBttn<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Let</SPAN> BttnTag(bt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br>  strBttnTag = bt<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br>  <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Get</SPAN> BttnTag() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    BttnTag = strBttnTag<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br>  <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> objOptBttn_Click()<br>  <SPAN style="color:#007F00">'// Pass the value back to the form's variable//</SPAN><br>  ParentForm.OptSelected = BttnTag<br>  <br>  ParentForm.Caption = "Current selected: " & BttnTag<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

In frmTest's Module:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Private</SPAN> strOptSelected <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#007F00">'// Declare as array and redim when we know how many buttons  //</SPAN><br><SPAN style="color:#00007F">Private</SPAN> cls() <SPAN style="color:#00007F">As</SPAN> Class1<br>  <br><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Let</SPAN> OptSelected(os <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br>  strOptSelected = os<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br>  <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Get</SPAN> OptSelected() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    OptSelected = strOptSelected<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN><br>  <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>  <SPAN style="color:#00007F">If</SPAN> Len(OptSelected) <SPAN style="color:#00007F">Then</SPAN><br>    Me.CommandButton1.Caption = OptSelected<br>  <SPAN style="color:#00007F">Else</SPAN><br>    Me.CommandButton1.Caption = "empty"<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>  <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br><SPAN style="color:#00007F">Dim</SPAN> Bttn <SPAN style="color:#00007F">As</SPAN> MSForms.OptionButton<br><SPAN style="color:#00007F">Dim</SPAN> n <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> lLastRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br>  <SPAN style="color:#00007F">With</SPAN> ThisWorkbook.Worksheets("Adressen")<br>    <SPAN style="color:#007F00">'// Use .Rows.Count so that when converted to Excel 2007 or later...//</SPAN><br>    lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>  <br>  <SPAN style="color:#007F00">'// Size the array  //</SPAN><br>  <SPAN style="color:#00007F">ReDim</SPAN> cls(1 <SPAN style="color:#00007F">To</SPAN> lLastRow)<br>  <br>  <SPAN style="color:#00007F">For</SPAN> n = 1 <SPAN style="color:#00007F">To</SPAN> lLastRow<br>    <SPAN style="color:#00007F">Set</SPAN> Bttn = Me.Controls.Add("Forms.OptionButton.1", "opt" & n, <SPAN style="color:#00007F">True</SPAN>)<br>    <SPAN style="color:#00007F">With</SPAN> Bttn<br>      .Top = ((n * 18) + 6) - 18<br>      .Left = 6<br>      .Width = 200<br>      .Caption = ThisWorkbook.Worksheets("Adressen").Cells(n, 1).Value<br>      .Tag = ThisWorkbook.Worksheets("Adressen").Cells(n, 1).Value<br>      .GroupName = "Custs"<br>      <SPAN style="color:#00007F">Set</SPAN> cls(n) = <SPAN style="color:#00007F">New</SPAN> Class1<br>      <SPAN style="color:#00007F">With</SPAN> cls(n)<br>        <SPAN style="color:#00007F">Set</SPAN> .ParentForm = Me<br>        <SPAN style="color:#00007F">Set</SPAN> .OptBttn = Bttn<br>        .BttnTag = Bttn.Tag<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN><br>End <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope that helps,

Mark
 
Upvote 0
ACK!

I forgot to mention: I would use the form's Initialize event instead of the Activate event.

Mark
 
Upvote 0
Thanks for the feedback and you are most welcome :-)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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