Checkboxes are one of the easiest ways to create interaction with users. Controlling them with VBA makes them more flexible and more useful in Excel applications.
Here is a collection of the most common code snippets and settings.
Create Check Boxes
The following code will create a checkbox
Sub CreateCheckBoxes() 'Create variable Dim chkBox As CheckBox 'Create checkbox Set chkBox = ActiveSheet.CheckBoxes.Add(Top:=0, Height:=1, Width:=1, Left:=0) End Sub
Loop through all the checkboxes
If you want to apply the same settings or values to all the checkboxes on a worksheet this can be achieved by looping through the checkboxs and applying the settings individually.
Sub LoopThroughCheckboxes() 'Create variable Dim chkBox As CheckBox 'Loop through each check box on active sheet For Each chkBox In ActiveSheet.CheckBoxes 'Do something to each checkbox using chkBox. Next chkBox End Sub
Set checkbox to a variable
The macro below will set a checkbox to a variable.
Sub SetCheckboxToVariable() 'Create variable Dim chkBox As CheckBox 'Set the variable to a specific checkbox Set chkBox = ActiveSheet.CheckBoxes("CheckBoxName") End Sub
Do you know the fastest way to learn foreign languages? It is to read, write, speak, and think in that language as often as possible. Apart from speaking, programming languages are no different. The more you immerse yourself in that language, the faster you will pick it up.
Therefore, what most people like you need is lots of examples that you can practice. That is why the 100 Excel VBA Macros eBook exists. It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers. The book contains:
- 100 example codes to practice reading and writing macros that will embed the language into your thinking.
- An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
- Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
- Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.
Common checkbox settings
The settings you are most likely to need to change are included in the macro below.
Sub CommonCheckboxSettings() 'Create variable Dim chkBox As CheckBox 'Set the variable to a specific checkbox Set chkBox = ActiveSheet.CheckBoxes("CheckBoxName") 'Set the variable to the name of the Checkbox calling the macro Set chkBox = ActiveSheet.CheckBoxes(Application.Caller) 'Set the checkbox name chkBox.Name = "CheckBoxName" 'Set the value of a check box (3 possible values) chkBox.value = xlOff chkBox.value = xlOn chkBox.value = xlMixed 'Set the shading of the can to True or False chkBox.Display3DShading = True chkBox.Display3DShading = False 'Set the linked cell chkBox.LinkedCell = "$E$5" 'Reset the linked cell to nothing chkBox.LinkedCell = "" 'Set position of the checkbox With chkBox .Top = 20 .Left = 20 .Height = 20 .Width = 20 End With 'Change the checkboxes caption chkBox.Caption = "check box caption" 'Display the name of the sheet containing the checkbox MsgBox chkBox.Parent.Name 'Display the address of the cell with the top left pixel 'of the check box MsgBox chkBox.TopLeftCell.Address 'Display the address of the cell with the bottom right pixel 'of the check box MsgBox chkBox.BottomRightCell 'Set the macro to be called with clicking the checkbox chkBox.OnAction = "NameOfMacro" 'Remove the macro being called chkBox.OnAction = "" 'Is the checkbox enabled? True or False chkBox.Enabled = False chkBox.Enabled = True 'Set the checkbox to not move with cells chkBox.Placement = xlFreeFloating 'Set the checkbox to move with the cells chkBox.Placement = xlMove 'Set the print option of the checkbox True or False chkBox.PrintObject = True chkBox.PrintObject = False 'Delete the checkbox chkBox.Delete End Sub
Delete all the checkboxes on the active sheet
Sub DeleteAllCheckBoxes() ActiveSheet.CheckBoxes.Delete End Sub
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: