Group Option buttons in different frames

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
Can anyone tell me if it is possible to group Option buttons that are in different frames on a userform?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If you have numerous Frames\OptionButtons, It would be easier to use a generic Class Module to sink the OptionButtons Change Event.

Here is an example that I have just tested.

Add a few frames to the UserForm and add a few OptioButtons on each frame .

Code:

1- Add a new Class module, give the class module the name of C_OptionEvents and place the following code in it:
Code:
Option Explicit

Public WithEvents OptBtn As MsForms.OptionButton
Private oUForm As UserForm
Private DisableEvents As Boolean

Public Property Get GetUserForm() As UserForm
    Set GetUserForm = oUForm
End Property

Public Property Set GetUserForm(ByVal vNewValue As UserForm)
    Set oUForm = vNewValue
End Property

Private Sub OptBtn_Change()
    Dim oCtrl As Control
    
    For Each oCtrl In GetUserForm.Controls
        If TypeOf oCtrl Is MsForms.OptionButton Then
            If oCtrl.Tag = "Grouped" Then
                If Not oCtrl Is OptBtn Then
                    If DisableEvents = False Then
                        oCtrl.Value = Not OptBtn.Value
                    End If
                End If
            End If
        End If
    Next
    DisableEvents = True
End Sub

2- Code in the UserForm Module:
Code:
Option Explicit

Private oCollection As New Collection

Private Sub UserForm_Initialize()
    Dim oCtrl As Control
    Dim oClassInstance As C_OptionEvents

    For Each oCtrl In Me.Controls
        If TypeOf oCtrl Is MsForms.OptionButton Then
            oCtrl.Tag = "Grouped"
            Set oClassInstance = New C_OptionEvents
            Set oClassInstance.OptBtn = oCtrl
            Set oClassInstance.GetUserForm = Me
            oCollection.Add oClassInstance
        End If
    Next
End Sub

Once you launch the userform, clicking on any OptionButton will unselect all other OptionButtons regardless of where they happen to be located - Inside the same frame, on a different frame or on the userform itself.
 
Upvote 0
I'll give that a try - thank you!
Hi sharky12345,
I'm not very firm in code like you did - but it's waht I need (an enjoyed testing).
So, I followed your recommondation and inserted your code in may excel vba with two frames (3 options buttons in first frame, 1 option button in the second frame).
While testing the option buttons they first did what was expected.
But after some clicks, the one option button in the second frame freeze - I could change the option buttons in the first frame.

Please, do you have an idea where I am wrong?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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