Checkboxes placed onto the face of the worksheet are an excellent way for users to interact with a spreadsheet. Each checkbox can be linked to a cell, so that when clicked the value in the cell changes between TRUE and FALSE.
But if you have a lot of checkboxes to create it becomes a real pain; there are just too many settings to set for each checkbox. If I have to create more than 2, then I’m groaning inside because of the inefficient process to set-up each box individually.
So I created some VBA to use in my Personal Macro Book. When the macro runs it will create, link, unlock and align as many checkboxes as I need in seconds! That makes me happy 🙂
How the VBA code works
When the VBA code below is run it will display two Input Boxes.
From the first Input Box select the range of cells in which to create the checkboxes, then click OK. In the screenshot below, it will create 30 checkboxes, being one in each cell from A1 – C10.
The second Input Box (shown below) will set the default value.
- True or 1 = Checked
- False or 0 = Unchecked
Once completed click OK.
The macro will now create all the checkboxes, a fantastic time saver.
Be warned; if you select a large range (such as an entire column or row), it might take some time as Excel is creating the checkboxes one by one.
The VBA Code – create multiple checkboxes
Copy the following code into a standard module, then run the macro entitled CreateCheckBoxes.
Sub CreateCheckBoxes() 'Declare variables Dim c As Range Dim chkBox As CheckBox Dim ansBoxDefault as Long Dim chkBoxRange As Range Dim chkBoxDefault As Boolean 'Ingore errors if user clicks Cancel or X On Error Resume Next 'Use Input Box to select cells Set chkBoxRange = Application.InputBox(Prompt:="Select cell range", _ Title:="Create checkboxes", Type:=8) 'Exit the code if user clicks Cancel or X If Err.Number <> 0 Then Exit Sub 'Use MessageBox to select checked or unchecked ansBoxDefault = MsgBox("Should the boxes be checked?", vbYesNoCancel, _ "Create checkboxes") If ansBoxDefault = vbYes Then chkBoxDefault = True If ansBoxDefault = vbNo Then chkBoxDefault = False If ansBoxDefault = vbCancel Then Exit Sub 'Turn error checking back on On Error Goto 0 'Loop through each cell in the selected cells For Each c In chkBoxRange 'Create the checkbox Set chkBox = chkBoxRange.Parent.CheckBoxes.Add(0, 1, 1, 0) With chkBox 'Set the position of the checkbox based on the cell .Top = c.Top + c.Height / 2 - chkBox.Height / 2 .Left = c.Left + c.Width / 2 - chkBox.Width / 2 'Set the name of the checkbox based on the cell address .Name = c.Address 'Set the linked cell to the cell with the checkbox .LinkedCell = c.Offset(0, 0).Address(external:=True) 'Enable the checkBox to be used when worksheet protection applied .Locked = False 'Set the caption to blank .Caption = "" End With 'Set the cell to the default value c.Value = chkBoxDefault 'Hide the value in the cell with Number Formatting c.NumberFormat = ";;;" Next c End Sub
In the code above I have assumed certain settings:
- The caption should be blank, as I normally use the cell next to the checkbox as the caption.
- The checkbox is not locked, therefore when the worksheet is protected the checkbox will still function.
- The name of the checkbox is the address of the cell in which it sits.
- The linked cell is the same as the cell containing the checkbox, but the number format will hide the value form the user.
- The location of the checkbox is centered in the cell.
The VBA Code – Delete multiple checkboxes
Having created a lot of checkboxes, the next problem you could face is how to delete a lot of checkboxes.
Delete all the checkboxes
The following VBA code will delete all the checkboxes on the active sheet.
Sub DeleteAllCheckBoxes() ActiveSheet.CheckBoxes.Delete End Sub
Whilst this will delete all the checkboxes, it will not remove the value from the linked cell. Therefore the following macro may be more appropriate.
Delete some checkboxes
If you only want to delete some checkboxes, then the following VBA code will do the trick. An Input Box will ask for a range of cells; these are the cells from which the checkboxes will be deleted.
Sub DeleteCheckBoxesInRange() 'Create variables Dim c As Range Dim chkBox As CheckBox Dim chkBoxRange As Range 'Ingore errors if user clicks Cancel or X On Error Resume Next 'Use Input Box to select cells Set chkBoxRange = Application.InputBox(Prompt:="Select cell range:", _ Title:="Delete checkboxes", Type:=8) 'Exit the code if user clicks Cancel or X If err.Number <> 0 Then Exit Sub 'Turn error checking back on On Error Goto 0 'Look through each checkbox For Each chkBox In chkBoxRange.Parent.CheckBoxes 'Delete checkbox where the cell containing the checkbox intersects with the selected range If Not Application.Intersect(Range(chkBox.TopLeftCell.Address), _ chkBoxRange) Is Nothing Then 'Clear the linked cell including formatting chkBox.Parent.Range(chkBox.LinkedCell).Value = "" chkBox.Parent.Range(chkBox.LinkedCell).NumberFormat = "General" 'Delete the check box chkBox.Delete End If Next chkBox End Sub
Note: In the macro above it is the top left pixel of the checkbox which determines it’s location. Even if the checkbox overspills into multiple cells, from a VBA perspective it is only within the cell containing to top left pixel.
About the author
Hey, I’m Mark, and I run Excel Off The Grid.
My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.
In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).
Do you need help adapting this post to your needs?
I'm guessing the examples in this post don't exactly match 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: