Option Button Click Event VBA

rossaba

New Member
Joined
Feb 24, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have 2 option button groups. When I click on an option button in the first group I would like the macro to control which option button is selected in the second group.

Group 1
Option Button 1
Option Button 2

Group 2
Option Button 3
Option Button 4

I would like the VBA code to do the following:
When option button 1 is clicked the macro will automatically select option button 4.

What is the VBA code that will trigger option button 4 to be selected or clicked?

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Are these controls on a worksheet or a userform? Assuming the former, because you refer to groups and the form control for a worksheet is called a Group Box, whereas the equivalent userform control is called a Frame, try this code in a standard module:
VBA Code:
Public Sub Setup_OptionButtons_OnAction()

    Dim i As Long
    
    With ActiveSheet
        For i = 1 To .OptionButtons.Count
            .OptionButtons(i).OnAction = "OptionButton_Click"
        Next
    End With

End Sub


Public Sub OptionButton_Click()
    
    Dim clickedOptionButton As OptionButton
    Dim selectOptionButton As OptionButton
    
    With ActiveSheet
    
        Set clickedOptionButton = .OptionButtons(Application.Caller)
        
        Select Case clickedOptionButton.Name
            Case Is = "Option Button 1"
                Set selectOptionButton = .OptionButtons("Option Button 4")
                selectOptionButton.Value = True
            Case Is = "Option Button 2"
                Set selectOptionButton = .OptionButtons("Option Button 3")
                selectOptionButton.Value = True
        End Select
        
    End With
    
End Sub
Run the Setup_OptionButtons_OnAction routine once to create the OnAction click handler for all the option buttons. The OptionButton_Click routine expects the option buttons are named exactly as you've posted.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

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