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 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 Input Box to set the default value of True / False chkBoxDefault = Application.InputBox _ (Prompt:="Default value:" & vbNewLine & " - True = Checked" & _ vbNewLine & " - False = Unchecked", Title:="Create checkboxes", Type:=4) 'Exit the code if user clicks Cancel or X If Err.Number <> 0 And chkBoxDefault = "" 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