You are here:

Excel/Using Basic Macros - Checkboxes

Advertisement


Question
"Hi there,

I am brand new to VB, but have been using Excel for quite some time. I have a spreadsheet with multiple tabs. One column in each tab needs to have almost the entire column (minus headers)with a checkbox in it. This does not have to link to anything or do any calculations. All i need is to have the end user able to check and uncheck the box. I am unable to get the checkbox feature into all my cells unless i copy and paste (or insert using forms function)each one individually? Do you know of a way to a) insert the checkbox into particular chunks of cells, and b) ensure that all checkboxes line up at the same point in each cell?  As well i would like to know how to get the check box to be deleted or inserted with each addition or deletion of rows?

Thanks.

Answer
Select the range where you want checkboxes and run the macro:

Sub AddCheckboxes()
Dim cb As CheckBox
Dim cell As Range
' delete existing boxes
For Each cb In ActiveSheet.CheckBoxes
 cb.Delete
Next
Selection.EntireRow.RowHeight = 22.5
For Each cell In Selection
  cell.Select
 Set cb = ActiveSheet.CheckBoxes.Add( _
   Left:=cell.Left, _
   Top:=cell.Top, _
   Width:=cell.Width, _
   Height:=cell.Height)
 cb.Caption = ""
Next
Selection.EntireColumn.ColumnWidth = 2.5
End Sub


The macro should be placed in a general module, not the sheet module or the Thisworkbook module.  

Since it clears all existing checkboxes on the sheet, you can run the macro whenever you have made changes.

There is no good event for trapping when a row has been deleted in most versions of Excel and None for when one has been added as I recall.  

--
regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2012 About.com, a part of The New York Times Company. All rights reserved.