Professional Documents
Culture Documents
The Complete Guide To Excel VBA ActiveX Checkboxes
The Complete Guide To Excel VBA ActiveX Checkboxes
The Complete Guide To Excel VBA ActiveX Checkboxes
(/vba/)
Table of Contents
1. Introduction
2. Available Properties
3. Renaming a Checkbox
4. Check the Value of a Checkbox
5. Checking/Unchecking
6. Enabling/Disabling Checkboxes
7. Hiding/Unhiding Checkboxes
8. Editing Label
9. Adding/Deleting Checkboxes
10. Selecting a Checkbox
11. Linking to a Cell
12. Checkbox Font
13. Checkbox Color
14. Changing Other Properties
15. Run Macro when Checkbox is Clicked
16. Run Macro when Mouse Hovers over Checkbox
17. Other Checkbox Events
18. Looping Through All Checkboxes
Introduction
This complete guide shows you how to control Excel ActiveX Checkboxes using VBA. If you want to learn how to control Form Control Checkboxes,
instead, head over to my Excel VBA Form Control Checkboxes Guide (/vba/2015/excel/complete-guide-to-excel-vba-form-control-checkboxes/).
ActiveX checkboxes are one of the two types of checkboxes you can add directly to your worksheet. The ActiveX checkboxes are located under
I made a sample product survey form using Excel ActiveX Checkboxes. I’ll point to this survey form throughout this guide as I use VBA macros to
Keep reading to learn how to grab the value, check, uncheck, change properties and loop through all ActiveX checkboxes!
Available Properties
Unlike Form Control Checkboxes, you can right-click ActiveX Checkboxes and pull up a list of properties:
ActiveX Checkbox Properties
All these properties can be changed in Excel using VBA - I’ll show you how.
Renaming a Checkbox
The checkbox name can be found beside the row labeled (Name) in the Properties Window (/vba/2015/excel/complete-guide-to-excel-vba-ActiveX-
checkboxes/#properties). A version of the name is found in the upper left of Excel when you have your checkbox selected, but here are two cautions
1. ActiveX controls cannot have spaces in their names. That’s why Method 1, below, works. Even if you put a space in the name in the upper left,
the (Name) attribute in the Properties Window (/vba/2015/excel/complete-guide-to-excel-vba-ActiveX-checkboxes/#properties) will still not
have the space.
2. You must click Design Mode on the Developer Tab (/vba/excel/enable-developer-tab/) to select your checkbox
CheckBox1
Method 1
Sub CheckboxName_Method1()
ActiveSheet.CheckBox1.Name="CheckBoxPrice"
End Sub
Method 2
Sub CheckboxName_Method2()
ActiveSheet.OLEObjects("CheckBox1").Name="CheckBoxPrice"
End Sub
way to check the value of an ActiveX checkbox using VBA. I’ll show you two ways. The following macros return the value of a checkbox named
Method 1
Sub CheckboxValue_Method1()
Dim cbValue As Variant
cbValue = ActiveSheet.CheckBox1.Value
End Sub
Method 2
Sub CheckboxValue_Method2()
Dim cbValue As Variant
cbValue = ActiveSheet.OLEObjects("CheckBox1").Object.Value
End Sub
to help you make your own macros like this one - we’ll send a copy, along with our Big Book of Excel VBA Macros, to your email address below.
Notice, the syntax of these macros is slightly different than the macros for Form Control Checkboxes (/vba/2015/excel/complete-guide-to-excel-
vba-form-control-checkboxes/#value). ActiveX Controls fall into the OLEObjects category, which are a subset of the Shapes collection we used when
Each method produces the same result, so you can use whichever one you feel the most comfortable with. There are three possible values of a
checkbox.
False Unchecked
If you’re trying to read the value of a checkbox using a data type (/vba/excel/vba-data-types/) Boolean, you will receive an error if the value is Null.
That’s why I chose a Variant data type in the macro examples above.
following macros first check the box, then uncheck the box and, finally, nullify the checkbox:
Method 1
Sub Check_Method1()
'Check the checkbox
ActiveSheet.CheckBox1.Value = True
'Uncheck the checkbox
ActiveSheet.CheckBox1.Value = False
'Gray the checkbox
ActiveSheet.CheckBox1.Value = Null
End Sub
Method 2
Sub Check_Method2()
'Check the checkbox
ActiveSheet.OLEObjects("CheckBox1").Object.Value = True
'Uncheck the checkbox
ActiveSheet.OLEObjects("CheckBox1").Object.Value = False
'Gray the checkbox
ActiveSheet.OLEObjects("CheckBox1").Object.Value = Null
End Sub
One thing worth noting is that when you programatically change the state of the checkbox to True or False, it calls the Click
(/vba/2015/excel/complete-guide-to-excel-vba-ActiveX-checkboxes/#click) event for that checkbox. In other words, if you have a macro that runs
when you click your checkbox on your spreadsheet, it may run when you change its value using VBA.
checkbox prevents the user from changing the value of the checkbox by clicking (checking and unchecking). You would still be able to change the
value by using VBA. Here are some examples for disabling ActiveX checkboxes:
Method 1
Sub DisableCheckbox()
ActiveSheet.CheckBox1.Enabled = False
End Sub
Method 2
Sub DisableCheckbox2()
ActiveSheet.OLEObjects("CheckBox1").Object.Enabled = False
End Sub
Method 3
Sub DisableCheckbox3()
ActiveSheet.OLEObjects("CheckBox1").Enabled = False
End Sub
I know, I know. It makes no sense that you get the same outcome whether or not you use the .Object keyword. This works for some properties,
Recall that disabling the checkboxes did not disable the macro associated with it. ActiveX checkboxes are different. Disabling the checkbox does
To enable your checkbox again, just set the .Enabled property back to True using whichever method you prefer. Here’s an example:
Sub EnableCheckbox()
ActiveSheet.CheckBox1.Enabled = True
End Sub
Method 1
Sub HideCheckbox()
ActiveSheet.CheckBox1.Visible = False
End Sub
Method 2
Sub HideCheckbox2()
ActiveSheet.OLEObjects("CheckBox1").Visible = False
End Sub
Again, I know it makes no sense, but you cannot use the .Object notation to change the visibility, like you could to enable/disable the checkbox.
Silly fact.
To make your checkbox visible again, change the .Enabled property back to True using whichever method you prefer. Here’s an example:
Sub ShowCheckbox()
ActiveSheet.CheckBox1.Visible = True
End Sub
“Price” to “Reliability.”
Method 1
Sub ChangeCaption()
ActiveSheet.CheckBox1.Caption = "Reliability"
End Sub
Method 2
Sub ChangeCaption2()
ActiveSheet.OLEObjects("CheckBox1").Object.Caption = "Reliability"
End Sub
Reliability
If using the OLEObjects method, the .Object method must be used to change the caption. Otherwise, you’ll get a runtime error.
Adding/Deleting a Checkbox
To add an ActiveX Checkbox, you need to know where you want to place the checkbox and what size you want it to be.
Add a Checkbox
Sub AddCheckbox()
With ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _
Left:=51.75, Top:=183, Width:=120, Height:=19.5)
.Name = "NewCheckBox"
.Object.Caption = "Reliability"
End With
End Sub
Notice how this macro combines the .Name and .Caption properties we discussed earlier to make a new checkbox with a custom label and
custom name.
Delete a Checkbox
Sub DeleteCheckbox()
ActiveSheet.OLEObjects("NewCheckBox").Delete
End Sub
Selecting a Checkbox
To select a checkbox titled CheckBox5 on your spreadsheet, try the following macro:
Method 1
Sub SelectCheckbox()
ActiveSheet.CheckBox5.Select
End Sub
Method 2
Sub SelectCheckbox2()
ActiveSheet.OLEObjects("CheckBox5").Select
End Sub
You won’t actually see that the checkbox is selected until you enter Design Mode on your Developer Tab (/vba/excel/enable-developer-tab/).
Method 1
Sub LinkCheckbox()
ActiveSheet.CheckBox5.LinkedCell = "Sheet1!$A$1"
End Sub
Method 2
Sub LinkCheckbox2()
ActiveSheet.OLEObjects("CheckBox5").LinkedCell = "Sheet1!$A$1"
End Sub
Now, cell “A1” in “Sheet1” changes to “TRUE” when CheckBox5 is checked, “FALSE” when unchecked and “#N/A” when Null.
font size and other font properties of an ActiveX Checkbox named CheckBox1 :
Method 1
Sub ChangeFont()
With ActiveSheet.CheckBox1
.Font.Name = "Times New Roman"
.Font.Size = 14
.Font.Italic = True
.Font.Bold = True
.Font.Underline = True
.Font.Strikethrough = True
End With
End Sub
Method 2
Sub ChangeFont2()
With ActiveSheet.OLEObjects("CheckBox1").Object
.Font.Name = "Times New Roman"
.Font.Size = 14
.Font.Italic = True
.Font.Bold = True
.Font.Underline = True
.Font.Strikethrough = True
End With
End Sub
Play around with the properties in the examples above until you get the look you desire. Using the .Font object, you can change the actual font of
the checkbox caption, the size of the font and whether or not the font is italicized, bolded, underlined or struck through. Notice how this only changes
Although most of the macros in this guide show you how to change properties of your ActiveX Checkboxes using VBA, this is a good time to remind
you the macros work both ways. For example, if you’re interested in seeing what value a property is set to, just change the order of the equality:
Sub CheckFont()
MsgBox ActiveSheet.CheckBox5.Font.Name
End Sub
following macro examples change the font color and background color of the ActiveX Checkbox named CheckBox1 :
Method 1
Sub ChangeColor()
With ActiveSheet.CheckBox1
.ForeColor = RGB(0, 0, 255)
.BackColor = RGB(200, 200, 200)
End With
End Sub
Method 2
Sub ChangeColor2()
With ActiveSheet.OLEObjects("CheckBox1").Object
.ForeColor = RGB(0, 0, 255)
.BackColor = RGB(200, 200, 200)
End With
End Sub
The .ForeColor property changes the font color, while the .BackColor property changes the background color of your checkbox. If you find
that your checkbox does not appear centered in your background color, adjust the .Height property of your checkbox.
If you want to change other properties listed in the Properties Window (/vba/2015/excel/complete-guide-to-excel-vba-ActiveX-
checkboxes/#properties), you can follow one of the above examples, but change the property portion of the code to the property you want to change
Sub ChangeProperties()
With ActiveSheet.CheckBox1
.Alignment = 0 'Move checkbox to the right of the caption
.SpecialEffect = 0 'Flat checkbox
.BackStyle = 0 'Transparent background
.TextAlign = 3 'Align text to the right
.TripleState = True 'Allow checkbox to toggle to Null
End With
End Sub
Wacky Checkbox
You can change the size and position of the checkbox in much the same way.
It’s important that you paste this template in the Microsoft Excel Object associated with where your checkbox is located! My checkbox is located on
Here’s an example. If your checkbox is named cbSelect , your macro would look like:
CheckBox1 checkbox.
Private Sub CheckBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Place Your Macro Here
End Sub
Remember, this template must be pasted in the Microsoft Excel Object that matches the worksheet where your checkbox is located! My checkbox is
Play around with these events to create some powerful spreadsheet interfaces.
checkboxes are checked, unchecked or null. Be sure to read the macro comments to understand how you can modify the VBA macro.
Sub CheckboxLoop()
Dim objX As OLEObject
I put a lot of effort into this tutorial and I hope you found it helpful. If you did, please subscribe using the form below and I’ll send you more
copy along with our VBA Developer Kit, loaded with VBA tips, tricks and shortcuts.
Before we go, I want to let you know we designed a suite of VBA Cheat Sheets (/vba/vba-
We included over 200 tips and 140 macro examples so they have everything you need to
(/vba/vba-cheat-sheets/bundle/? New
source=bottomimage) to
The VBA Tutorials Blog with Ryan Wells Privacy Policy Each VBA Tutorial contains macro examples to help you
(/vba/privacy-policy/) learn VBA code quickly. Follow these VBA tutorials to
The Complete Guide to Excel VBA ActiveX RSS (/vba/feed.xml)
learn basic and advanced Excel macro programming.
Checkboxes Twitter
(https://twitter.com/ry
anwellsr)
GitHub
(https://github.com/ry
anwellsr)
LinkedIn
(https://linkedin.com/i
n/ryanwellsr)