Multiple OptionButton Groups on Same Userform

sarasotavince

New Member
Joined
May 14, 2011
Messages
24
Hello one and all:

I have multiple optionbutton groupings on the same userform. I am using the group name in the properties box (set1, set2, set3, etc) so that only one choice can be made from each group. I can write code to get the first group to operate but I cannot figure out how to use the group name to control the code for each additional grouping (set1, set2, set2, etc). Can someone help me modify this code to reflect only a specific group (say, set1) and I can repeat the process with each group. Much thanks.

For i = 1 To 12 'THIS IS THE FIRST SET OF OPTIONBUTTONS
If Me.Controls("OptionButton" & i).Value = True Then<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
MsgBox "Found it" 'OR WRITE THE CAPTION VALUE TO SOME SHEET
Exit For<o:p></o:p>
End If<o:p></o:p>
Next i

I figure there has to be a way to use the group name to control the code. I will end up having 6 or 7 groups and each selection from within each group upon exit will do something different. (I can handle this part).

One last thing. I do not want to use frames. And wouldn't the code be basically the same using a frame name instead of a group name? The reason I ask is because I might want to use a frame for this very same problem on another project.

Thanks so much.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You could use something like this. If you use this, note that OptButtonChosenFromGroup will return Nothing if no button from the group is selected.

Code:
Function OptButtonChosenFromGroup(groupName As String) As msforms.OptionButton
    Dim oneControl As Object
    For Each oneControl In Me.Controls
        If TypeName(oneControl) = "OptionButton" Then
            If oneControl.Value And oneControl.groupName = groupName Then
                Set OptButtonChosenFromGroup = oneControl
                Exit Function
            End If
        End If
    Next oneControl
End Function

Private Sub UserForm_Click()
    MsgBox OptButtonChosenFromGroup("set1").Name & " chosen." _
        & vbCr & OptButtonChosenFromGroup("set2").Name & " chosen."
End Sub
 
Upvote 0
Thanks mikerickson !!


I studied your code and was able to adapt it to my situation. Since I need to write the caption from each set to a WORKSHEET, I was able to use:


Sheets("SHEETNAME").Cells(11, 10).Value = OptButtonChosenFromGroup("set1").Caption


You rock.
 
Upvote 0

Forum statistics

Threads
1,215,532
Messages
6,125,367
Members
449,221
Latest member
chriscavsib

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