Create multiple checkboxes instantly with VBA

Create multiple checkboxes

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.

Create Checkboxes - Select Range

The second Input Box (shown below) will set the default value.

  • True or 1 = Checked
  • False or 0 = Unchecked

Once completed click OK.

Create Checkboxes - Default Value

The macro will now create all the checkboxes, a fantastic time saver.

Create Checkboxes - After

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.

Generate accurate VBA code in seconds with AutoMacro

AutoMacroExample

AutoMacro is a powerful VBA code generator that comes loaded with an extensive code library and many other time-saving tools and utilities.

Whether you’re an experienced coder looking to save time, or a newbie just trying to get things to work, AutoMacro is the tool for you.

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.

Don’t forget:

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:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. 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.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

9 thoughts on “Create multiple checkboxes instantly with VBA

  1. Anand Pillay says:

    Excel VBA:

    If I select all the child check box then parent check box should automatically get select.

    If any one of the child check box is Unchecked then parent check box get unchecked automatically

    Please help me with VBA code.

  2. David Lin says:

    Great Script…. Downloaded and was perfect. Many Thanks!!!
    However, something wrong with the line “If Err.Number 0 And chkBoxDefault = “” Then Exit Sub.”
    I dont know why. I just deleted it and the rest is what I need.

    • Excel Off The Grid says:

      Thanks David – It’s caused by the True/False nature of closing the Inputbox. I have now updated the post to use message box to select the Checked/Unchecked option.

  3. Sean says:

    The check boxes are coming up only half a box? What part of the code do I need to modify? I tried even on large cells but still comes up very small and have to right click and drag each one to increase size.

Leave a Reply

Your email address will not be published. Required fields are marked *