Checkbox

 

Draw a Checkbox    |   Refer to Checkbox in your Code

An Excel VBA checkbox is a field which can be checked to store information. Learn how to draw a checkbox on your worksheet and how to refer to a checkbox in your code.

Draw a Checkbox

Excel 2010 and Excel 2007 users. Click on Insert from the Developer tab and then click on Check Box in the ActiveX Controls Section.

Create an Excel VBA Checkbox in Excel 2010 or Excel 2007

Excel 2003 users. Click on Check Box from the Control ToolBox.

Create an Excel VBA Checkbox in Excel 2003

1. Draw a checkbox on your worksheet.

Excel VBA Checkbox Example

You can change the caption of the checkbox by right clicking on the checkbox and then clicking on Properties and Caption (make sure Design Mode is selected).

You can change the name of the checkbox by right clicking on the checkbox and then clicking on Properties and Name. If you choose to do this, remember to refer to this name in your code. it is good practice to change the names of controls. This will make your code easier to read when you have more controls. For now, we will leave Checkbox1 as the name of the checkbox since we only have one checkbox in this example.

Refer to Checkbox in your Code

To refer to a checkbox in your Excel VBA code, execute the following steps:

1. Right click on the checkbox. Click on View Code. Add the following code lines:

Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then Range("C2").Value = 1
If CheckBox1.Value = False Then Range("C2").Value = 0

End Sub


2. Exit the Visual Basic Editor and check the checkbox.

Result:

Checked Unchecked

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

Go to Top: Checkbox    |   Go to Next Topic: Option Buttons

Learn more >>
Login to our example programs and let us help you become an Excel VBA professional!