How to Create and Apply an Option Button Click Event in Excel VBA -2 Steps

Introduction to the Excel VBA Option Button

The option button refers to a type of icon that allows the user to choose just one option. You can add option buttons in Excel using the Form Controls or ActiveX Controls.


Create the option button to decide if a candidate is Selected or Rejected:

Step 1- Inserting an Option Button in Excel

  • Open the Excel worksheet.
  • Go to the Developer tab.
  • Select Controls.

Forming Option Button in Excel

  • Click Insert.
  • Click Option Button in ActiveX Controls.

  • Drag the first option button (OptionButton1) to the worksheet (see screenshot).

  • Select the option button and go to the Developer tab again.
  • Click Properties.

  • In the Properties window: select Alphabetic.
  • In Name, enter Option1.
  • In Caption, enter Pass.
  • Close the window.

You can see the CaptionPass’ on the option button.

In the Name Box, you can see Option1.

  • Insert the second option button in the same worksheet.
  • Enter Option2 in Name.
  • In Caption, enter Fail.

Forming Option Button in Excel

The second option button is displayed.


Step 2 -Assigning the Selected Option to Excel VBA

Assigning the Selected Option in Excel VBA

  • Click the first option button (Pass).
  • Go to the Developer tab.
  • Confirm that the Design Mode is active.

  • Right-click option button (Pass).
  • Select View Code.

Assigning the Selected Option in Excel VBA

  • The VBA code window will open.
  • Enter the following VBA code in the code window:
Private Sub Option1_Click()
If Option1.Value = True Then Range("D5").Value = "Selected"
End Sub

  • Click Save.

  • Go to the Developer tab to deactivate the Design Mode.

  • Click the Pass option button.

You will see the output (Selected) in D5.

  • For the second option button, select the option button (Fail) > go to the Developer tab > enable the Design mode.

Assigning the Selected Option in Excel VBA

  • Click View Code by right-clicking the option button (Fail).

  • Enter the VBA code below in the code window:
Private Sub Option2_Click()
If Option2.Value = True Then Range("D5").Value = "Rejected"
End Sub

  • Click Save.

  • Turn off the Design Mode option in the Developer tab.

  • If you click the Fail option button, D5 will show ‘Rejected’.

Assigning the Selected Option in Excel VBA

Read More: How to Set Option Button Value in Excel VBA


Download Practice Workbook

Download the practice workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Sagufta Tarannum
Sagufta Tarannum

Sagufta Tarannum, holding a BSc in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, contributes significantly as an Excel & VBA Content Developer at ExcelDemy. Fueled by a deep interest in research and innovation, she actively engages with Excel. In her role, Sagufta not only skillfully addresses challenging issues but also demonstrates enthusiasm and expertise in gracefully navigating intricate situations, underscoring her unwavering commitment to consistently delivering exceptional content. Her interests are Advanced... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo