Excel: Adding Checkboxes the Easy Way

Apparently I have trespassed against The Big Programmer In the Sky and I’m currently suffering in Coder’s Purgatory where they make me code in VBA. It could be worse. I could be in Programmer Hell (also known as the infamous 0xf th Circle of Hell) where you don’t get to code at all but instead attend meetings and team building events. I figured that since I’m suffering, I will make you suffer as well by exposing you to some VBA.

For example, how about we create something like this in Excel:

excel1.png

You know, like a checklist. The users would go down, the list, hit the appropriate checkboxes and then we would have a script that extracts this data, and builds an exception list. How many questions? Oh, not many - maybe like a 100 in total, spread across 4-5 worksheets. And the good news is that the questionnaire already exists in Word document so you can just like copy and paste it.

Each checkbox is linked to a cell and toggling it will toggle the value of the cell between TRUE and FALSE. Extracting data is trivial. The problem here is creating the checklist itself - it is a tedious data entry job you give to your intern after he/she finishes filing, and does the Starbucks run. There is no easy way to copy these checkboxes. Or rather you can copy them, but their cell references do not change. So you have to change the linked cell property for every single checkbox manually.

So I said “fuck this shit” and wrote a macro to do this for me. The following script will prompt the user for 3 things: the Cell Range which should be given like so A1:A10 which is the range where the checkboxes will be inserted, a Linked Column to be used for linked cells (the row number of the linked cell will be the same as the row number of where the checkbox was inserted) and a Checkbox Label (the text to be displayed next to the checkbox). Then it will generate the right number of checkboxes. Here is the code:

Option Explicit
Sub insertCheckboxes()
 
  Dim myBox As CheckBox
  Dim myCell As Range
 
  Dim cellRange As String
  Dim cboxLabel As String
  Dim linkedColumn As String
 
  cellRange = InputBox(Prompt:="Cell Range", _
    Title:="Cell Range")
 
  linkedColumn = InputBox(Prompt:="Linked Column", _
    Title:="Linked Column")
 
  cboxLabel = InputBox(Prompt:="Checkbox Label", _
    Title:="Checkbox Label")
 
  With ActiveSheet
    For Each myCell In .Range(cellRange).Cells
      With myCell
        Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _ 
          Width:=.Width, Left:=.Left, Height:=.Height)
 
        With myBox
          .LinkedCell = linkedColumn & myCell.Row
          .Caption = cboxLabel
          .Name = "checkbox_" & myCell.Address(0, 0)
        End With
 
        .NumberFormat = ";;;"
      End With
 
    Next myCell
  End With
End Sub

So here you go, inserting checkboxes the lazy way. Welcome to my purgatory, I hope you can feel my opain. Now excuse me while I go shoot myself in the face with a shotgun.

End With.

Related Posts:

  • Convert a large Access table into Excel files
  • Dump MySQL table into CSV file
  • Excel: Too many different cell formats
  • Excel in-cell Bar Charts
  • Parsing Excel Files with Perl
  • JQuery Quirks
  • Read Excel with PHP
  • VBA Hacks and Trickery Grab Bag
  • PHP: Export Query Results to a CSV File
  • 3 Value Checkbox with JQuery

  • 3 Responses to “Excel: Adding Checkboxes the Easy Way”

    1. Gravatar Dr. Azrael Tod GERMANY Says: Reply to this comment

      Another reason to not use Excel… (while i dont think the alternatives like OpenOffice or google docs are better at this)

      Maybe this is why spreadsheets are not used often for things like this. This and.. oh yeah.. everybody can edit the whole file, so if someone clicks the wrong field instead of another checkbox and types anything, there could have been something important which is now lost.

      Posted using Flock Flock 2.0b2 on Linux Linux
    2. Gravatar Luke Maciak UNITED STATES Says: Reply to this comment

      My sentiment exactly - this should not be done in excel, but tell that to my boss. If he could browse internet from within excel he probably would. P

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.16 on Ubuntu Linux Ubuntu Linux
    3. Gravatar Dr. Azrael Tod GERMANY Says: Reply to this comment

      Bureaucrats! -.-
      I hate them… they are the reason for the success of things like ms office or worse like this SAP/Abap-Crap i once had to deal with for short time. -.-

      Posted using Flock Flock 2.0b2 on Linux Linux

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]