Disable Option Button based on another Option Button, when all buttons are defined in a class

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Hi guys,

I am trying to diable an option button, dependant on a user selection.

I have my modules as follows:

ThisWorkbook:

Code:
Private Sub Workbook_Open()
    
    Dim ctrl As MSForms.Control
    Set col = New Collection
    
    ' This loop connects all buttons in UserForm
    ' to one event hanlder in EventsTrapper class.
    For Each ctrl In frmTest.Controls
        If TypeOf ctrl Is MSForms.OptionButton Then
            Set EventTrapper = New EventsTrapper
            Set EventTrapper.Button = ctrl
            Set EventTrapper.Form = frmTest
            col.Add EventTrapper
            Set EventTrapper = Nothing
        End If
    Next
End Sub

Standard:

Code:
Public col As Collection
Public EventTrapper As EventsTrapper

and EventsTrapper class module:

Code:
Public WithEvents Button As MSForms.OptionButton
Public Form As MSForms.UserForm
Private Sub Button_Change()
  With frmTest
    If .OptSum.Value = True Then
        .optSeaH.Enabled = False
    End If
End With
        
        
End Sub

I cant get the optSeaH to be disbaled when OptSum is true.

Any ideas?

Thanks in advance.
 
Code:
[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] Button_Change()
    [COLOR="Blue"]With[/COLOR] Form
        [COLOR="Blue"]If[/COLOR] Button.Value = [COLOR="Blue"]True[/COLOR] [COLOR="Blue"]Then[/COLOR]
            .optSeaH.Enabled = [COLOR="Blue"]False[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

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.
RoryA,

Thanks. I just have a couple of questions.

I am slightly confused by this line:

Private Sub btnController_Change()
' disable other option buttons if controller option is True
Button.Enabled = Not btnController.Value
End Sub

If I have a simplified example, with only a few option buttons. If I have for the controllers, OptSum and OptWin, and for the dependants optSeaL, optSeaM, optSeaH, so a total of 5.

And taking the case that if OptWin.Value = True, I wish to diasble optSeaL and enable optSeaM and optSeaH.

and if OptSum.Value = True, I wish to disable optSeaH and enable optSeaL and optSeaM.

I would have thought my code should be as follows:

Standard Module (named ClassControls)

Code:
Private Sub UserForm_Initialize()
   Dim EventTrapper As EventsTrapper
   Dim ctrl As MSForms.Control
   Set col = New Collection
   ' This loop connects all buttons in UserForm
   ' to one event hanlder in EventsTrapper class.
   For Each ctrl In Me.Controls
      If TypeName(ctrl) = "OptionButton" Then
         ' don't need to handle optsum
         If Not ctrl Is OptSum Then
            Set EventTrapper = New EventsTrapper
            Set EventTrapper.Button = ctrl
            Set EventTrapper.btn1Controller = OptSum
            col.Add EventTrapper
 
         If Not ctrl Is OptWin Then
            Set EventTrapper = New EventsTrapper
            Set EventTrapper.Button = ctrl
            Set EventTrapper.btnController = OptWin
            col.Add EventTrapper
         End If
         End If
      End If
   Next
End Sub

and in my class module (names EventsTrapper)

Code:
Private Sub btnController_Change()
   ' disable other option buttons if controller option is True
   If btnController.Value = True Then
    Button.optSeaL.Enabled = False
    End If
 
End Sub
Private Sub btn1Controller_Change()
   ' disable other option buttons if controller option is True
   If btn1Controller.Value = True Then
    Button.optSeaH.Enabled = False
    End If
 
End Sub
Private Sub Button_Change()
   ' code for other option buttons goes here
End Sub
 
I think if you can answer the above for me, I will then be able to work out the rest by myself. 
Sorry for the incessent questions
 
Upvote 0
In group there can be only one "checked" OptionButton.
 
Upvote 0
Oh my, I am now totally confused lol.

I have entered rorya's code exactly as written, and still none of the option buttons are becomming disabled.

Sorry guys, I am totally lost with all this now. Thanks for the help though :)
 
Upvote 0
At this point, I don't see any benefit whatsoever in doing the enabling inside the class. Just use the change event of those two option buttons in the form to disable the relevant controls.
 
Upvote 0
Yeah I have done that now. The reasons I wished to use a class is to update the variables linked to the other option buttons, and also try to get used to using classes that I don't really understand as you have seen...

Ah well.

Thanks for your time both of you
 
Upvote 0
You can still use the class to handle the option buttons and their values, it just doesn't really make sense to me to try and incorporate the disabling of the controls into the class.
 
Upvote 0

Forum statistics

Threads
1,225,155
Messages
6,183,215
Members
453,151
Latest member
Lizamaison

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