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
= <SPAN style="color:#00007F">New</SPAN> Class1<br> <SPAN style="color:#00007F">With</SPAN> cls
<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