Detect which option button on form is clicked

Srbin do jaja

New Member
Joined
Sep 5, 2011
Messages
7
I have a userform and around 400 option buttons in groups of 5 that are inside of frame (its a survey). Now i want to change background of each option button that is clicked. That means if optionbutton's value = true then optionbutton's background lets say becomes red.
 

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.
here is what i'm trying to do
Code:
Private Sub OptionButton1_AfterUpdate()
    OptionButton1.BackColor = RGB(255, 255, 0)
End Sub

Private Sub OptionButton2_AfterUpdate()
    OptionButton2.BackColor = RGB(255, 255, 0)
End Sub

Private Sub OptionButton3_AfterUpdate()
    OptionButton3.BackColor = RGB(255, 255, 0)
End Sub

but i dont want to write 400 of these routines. is there a better way?
 
Upvote 0
This is possible, yes. But the code for that is rather advanced: you need to use classes to handle controls and there events (like clicking a control such as an option button).

I would also think that you can have the same effects with "only" 40 optionbuttons or 20. Using a multipage control on a userform, you present each time 8 questions in the survey. Clicking "next page" (a command button that activates the next page) will show different questions. But in reality it's all the same controls (labels, option buttons, ...) that are cleared and presented again to the user.
 
Upvote 0
This is possible, yes. But the code for that is rather advanced: you need to use classes to handle controls and there events (like clicking a control such as an option button).

I would also think that you can have the same effects with "only" 40 optionbuttons or 20. Using a multipage control on a userform, you present each time 8 questions in the survey. Clicking "next page" (a command button that activates the next page) will show different questions. But in reality it's all the same controls (labels, option buttons, ...) that are cleared and presented again to the user.

well i have 90 questions split in 9 multipages, i do have buttons to navigate trough multipages. and i do need that advance code, since i aint that good at classes. dont know how to detect events inside of class.
 
Upvote 0
You will have a number of things to be set up:

1.

Call a procedure like GetOptionButtons in one of the Userform's events: for instance Userform_Initialize()

2.

That procedure (in a regular module) contains code like this:

Code:
Dim OB() As New OBclass

Sub GetOptionButtons()
    
    ReDim OB(1 To 3)
    For i = 1 To 3 '400
        Set OB(i).OBgroup = UserForm1.Controls("OptionButton" & i)
    Next

End Sub

3.

Add a Class module called OBclass. This class will contain following code:

Code:
Option Explicit

Public WithEvents OBgroup As MSForms.OptionButton

Private Sub OBgroup_Click()

    OBgroup.BackColor = vbRed 'RGB(255, 255, 0)

End Sub

That's it.
 
Upvote 0
This code above is all copy-pasted from an example I set up, which worked fine for me.
 
Upvote 0
Just to confirm wigi's code worked for me !!!!
I places the Sub GetOptionButtons() in the Userform1 initialize code as below, which help me.
If you have "Option Explicit" set as below you will need to also Dimension the variable "i", as shown
Code:
Option Explicit
Dim OB() As New OBclass
Private Sub UserForm_Initialize()
Dim i As Integer
'Sub GetOptionButtons()
    ReDim OB(1 To 8)
    For i = 1 To 8 '400
            Set OB(i).OBgroup = UserForm1.Controls("OptionButton" & i)
    Next
End Sub

The Class Procedure below must be placed in a Class Module (Not ordinary module)
When you Create a Class module as in this case it will show in the properties window of the VB code window as "Class1".
If you highlight it's name, then click "F4", its properties window will show , and you Must change its name to "OBclass"
Code:
Option Explicit
Public WithEvents OBgroup As MSForms.OptionButton
Private Sub OBgroup_Click()
      OBgroup.BackColor = vbRed 'RGB(255, 255, 0)
End Sub

Acknowledgement to wigi. !!
Regards Mick
 
Upvote 0
Here is an alternative Method that should work for all OptionButtons without the need for a Class Module. Still they all use a generic common event Procedure.

Place this in the UserForm Module and you will see that all the Option Buttons will turn green when clicking on them.

Code:
Option Explicit

Private WithEvents Ws As Worksheet

Private Sub UserForm_Initialize()

    Dim i As Long
    Dim oCtl As Control
    
    Application.EnableEvents = False
    Set Ws = ThisWorkbook.Worksheets.Add
    Ws.Visible = xlSheetHidden
    For Each oCtl In Controls
        If TypeName(oCtl) = "OptionButton" Then
            i = i + 1
            oCtl.ControlSource = Ws.Cells(i, 1).Address(, , , True)
            oCtl.Value = False
        End If
    Next
    Application.EnableEvents = True

End Sub

Private Sub Ws_Change(ByVal Target As Range)

    Dim oCtl As Control
    
    For Each oCtl In Controls
        If TypeName(oCtl) = "OptionButton" Then
            oCtl.BackColor = vbButtonFace
            oCtl.Value = False
        End If
    Next
    ActiveControl.BackColor = vbGreen
    ActiveControl.Value = True

End Sub


Private Sub UserForm_Terminate()

    With Application
        .DisplayAlerts = False
        Ws.Delete
        .DisplayAlerts = True
        .EnableEvents = True
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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