Userform - Only allow one OptionButton to be selected inside multipage within a Frame with other OptionButtons

PioSwgr

New Member
Joined
Dec 22, 2023
Messages
8
Office Version
  1. 365
Currently working on a userform and quite new to it. I have a multipage (with 2 pages of OptionButtons) inside a frame which also contains 4 other Optionbuttons. Is there a way to ensure that once I click a option button inside the mutlipage that it deselects the option button outside of it and the optionbuttons on the other pages? And vis versa? Is there a property I can enable or will I need to code this feature? Any thoughts or ideas would be appreciated!

Please disregard the cycle rating box as that is working as intended.
1704404597137.png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
It will need to be coded. Initialized with OptionButton_Click method. Using If Then, or Case Select statements.
 
Upvote 0
The GroupName property exists to separate optionbutton groups, but it must be in the same container (form, frame, page).
If they are in different containers, it can be a frame or a page, they will be in different groups even though they have the same group name.

Check the following:
1704416978363.png

1)
Then you can choose to put all the option buttons in the same container with the same groupname.​

2)
Or with the following code:​

Create a class module: Class1 and put the following code:

VBA Code:
Public WithEvents MultipleOptionButton As MSForms.OptionButton  '<--- At the beginning of all the code

Private Sub MultipleOptionButton_Click()
  Dim ctrl As MSForms.Control
  Dim s As String
 
  s = MultipleOptionButton.Caption
  For Each ctrl In UserForm1.Controls         'Fit to the name of your userform
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Caption <> s Then
        ctrl.Value = False
      End If
    End If
  Next
End Sub

In your Userform:

VBA Code:
Option Explicit             '<--- At the beginning of all the code
Dim OptBt() As New Class1   '<--- At the beginning of all the code

Private Sub UserForm_Initialize()
  Dim i As Long, ctrl As MSForms.Control
  i = 1
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Caption Like "CH -*" Then
        ReDim Preserve OptBt(i)
        Set OptBt(i).MultipleOptionButton = ctrl
        i = i + 1
      End If
    End If
  Next
End Sub
Note: The above code will work for all optionbuttons that start with the characters in the caption: "CH -"


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Last edited:
Upvote 0
The GroupName property exists to separate optionbutton groups, but it must be in the same container (form, frame, page).
If they are in different containers, it can be a frame or a page, they will be in different groups even though they have the same group name.

Check the following:

1)
Then you can choose to put all the option buttons in the same container with the same groupname.​

2)
Or with the following code:​

Create a class module: Class1 and put the following code:

VBA Code:
Public WithEvents MultipleOptionButton As MSForms.OptionButton  '<--- At the beginning of all the code

Private Sub MultipleOptionButton_Click()
  Dim ctrl As MSForms.Control
  Dim s As String
 
  s = MultipleOptionButton.Caption
  For Each ctrl In UserForm1.Controls         'Fit to the name of your userform
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Caption <> s Then
        ctrl.Value = False
      End If
    End If
  Next
End Sub

In your Userform:

VBA Code:
Option Explicit             '<--- At the beginning of all the code
Dim OptBt() As New Class1   '<--- At the beginning of all the code

Private Sub UserForm_Initialize()
  Dim i As Long, ctrl As MSForms.Control
  i = 1
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Caption Like "CH -*" Then
        ReDim Preserve OptBt(i)
        Set OptBt(i).MultipleOptionButton = ctrl
        i = i + 1
      End If
    End If
  Next
End Sub
Note: The above code will work for all optionbuttons that start with the characters in the caption: "CH -"


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Thank you Dante. The code does work... partially. What happens is once I select a option button with the caption "CH -", it turn all other options buttons values I had previously selected (value=true) on this userform, in other frames, to value=false. It appears to affect the entire userform when all it should do is be restricted to that one frame. Would there be a way to only use this code in the one frame? Appreciate the help

And unfortunately, the groupname did not work for me.
 
Upvote 0
it turn all other options buttons values I had previously selected (value=true) on this userform


So, I'm not understanding your original requirement:
Is there a way to ensure that once I click a option button inside the mutlipage that it deselects the option button outside of it and the optionbuttons on the other pages?


You must specify which pages or frames you want it to work on.
If possible, share your file on GoogleDrive, no information is necessary on the sheets, just the form.
What is important is that you explain which option you select and which ones you want to be deselected.

----- --
I hope to hear from you very soon and I will respond as soon as possible.
Cordially
Dante Amor
----- --
 
Upvote 0
So, I'm not understanding your original requirement:



You must specify which pages or frames you want it to work on.
If possible, share your file on GoogleDrive, no information is necessary on the sheets, just the form.
What is important is that you explain which option you select and which ones you want to be deselected.

----- --
I hope to hear from you very soon and I will respond as soon as possible.
Cordially
Dante Amor
----- --
Apologies, I should have mentioned the entire scope of this project/userform experience, the larger screenshot should assist. I am unable to share my file unfortunately. What happens with the code is once I select an option button with the caption "CH -", it deselects all other options buttons values in the other frames inside this userform, which I would like to leave unaffected. I only want one option button to be selected in each of these frames. This userform has many different frames, as shown in the picture below. Would the code be able to exclusively influence the Breaker Frame. Maybe by reducing the scope of ctrl to frame instead of userform? Thank you for the help thus far!

*If this helps, all option buttons in the breaker frame and multipage(inside the frame) are all named, OptionButtonBrkr followed by a number*

1704833077606.png
 
Last edited:
Upvote 0
I should have mentioned the entire scope of this project/userform experience, the larger screenshot should assist
I think it is enough to change this line in the class module:
VBA Code:
If ctrl.Caption <> s Then

By this line:
VBA Code:
If ctrl.Caption <> s And ctrl.Caption Like "CH -*" Then



-------
But you mentioned:

*If this helps, all option buttons in the breaker frame and multipage(inside the frame) are all named, OptionButtonBrkr followed by a number*

So instead of the above code use the following:

So in the class module put the following code:
VBA Code:
Public WithEvents MultipleOptionButton As MSForms.OptionButton  '<--- At the beginning of all the code

Private Sub MultipleOptionButton_Click()
  Dim ctrl As MSForms.Control
  Dim s As String
  
  s = MultipleOptionButton.Name
  For Each ctrl In UserForm1.Controls         'Fit to the name of your userform
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Name Like "OptionButtonBrkr*" Then
        If ctrl.Name <> s Then
          ctrl.Value = False
        End If
      End If
    End If
  Next
End Sub

In the form code:
VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long, ctrl As MSForms.Control
  i = 1
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Name Like "OptionButtonBrkr*" Then
        ReDim Preserve OptBt(i)
        Set OptBt(i).MultipleOptionButton = ctrl
        i = i + 1
      End If
    End If
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
🧙‍♂️
 
Upvote 1
Solution
I think it is enough to change this line in the class module:
VBA Code:
If ctrl.Caption <> s Then

By this line:
VBA Code:
If ctrl.Caption <> s And ctrl.Caption Like "CH -*" Then



-------
But you mentioned:



So instead of the above code use the following:

So in the class module put the following code:
VBA Code:
Public WithEvents MultipleOptionButton As MSForms.OptionButton  '<--- At the beginning of all the code

Private Sub MultipleOptionButton_Click()
  Dim ctrl As MSForms.Control
  Dim s As String
 
  s = MultipleOptionButton.Name
  For Each ctrl In UserForm1.Controls         'Fit to the name of your userform
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Name Like "OptionButtonBrkr*" Then
        If ctrl.Name <> s Then
          ctrl.Value = False
        End If
      End If
    End If
  Next
End Sub

In the form code:
VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long, ctrl As MSForms.Control
  i = 1
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "OptionButton" Then
      If ctrl.Name Like "OptionButtonBrkr*" Then
        ReDim Preserve OptBt(i)
        Set OptBt(i).MultipleOptionButton = ctrl
        i = i + 1
      End If
    End If
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
🧙‍♂️
Thank you very much, Dante Amor! My user form works as intended. Appreciate all the help!
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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