spot_img

Option Buttons

 

If you have more than one option button, only one of the option buttons can be selected. To create option buttons in Excel VBA, execute the following steps.

1. On the Developer tab, click Insert.

2. In the ActiveX Controls group, click Option Button.

Create an option button in Excel VBA

3. Drag two option buttons on your worksheet.

4. Right click the first option button (make sure Design Mode is selected).

5. Click View Code.

View Code

Note: you can change the caption and name of a control by right clicking on the control (make sure Design Mode is selected) and then clicking on Properties. Change the captions of the option buttons to Female and Male. For now, we will leave OptionButton1 and OptionButton2 as the names of the option buttons.

6. Add the following code line:

If OptionButton1.Value = True Then Range(“D3”).Value = 10

7. Right click the second option button (make sure Design Mode is selected).

8. Click View Code.

9. Add the following code line:

If OptionButton2.Value = True Then Range(“D3”).Value = 20

10. Click the option buttons on the sheet (make sure Design Mode is deselected).

Result:

Option Button 1 True

Option Button 2 True

Although in some situations it can be useful to directly place option buttons on your worksheet, option buttons are particularly useful when placed on a Userform.

spot_img
Previous articleWorkbook and Worksheet Object in Excel VBA
Next articleRead Data from a Text File using Excel VBA