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

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Worked with the program for many years - provided assistance on MS Excel Newsgroups since 1997. Have received the Microsoft MVP award annually since 1999. I don't answer questions on using Excel in a browser Since I have no way to test this. Prefer not to answer charting questions. I consider myself to be particularly knowledgeable about using VBA internal to Excel but have no problems with formulas and pivot tables either.

Experience

Have Used Excel for 15 - 20 years. Answered in excess of 70,000 Excel related questions in MS Excel newsgroups. Unless obvious, please specify whether you want a worksheet function or macro/VBA solution.

Education/Credentials
BS General Engineering (concentration in Industrial Engineering) MS Operations Research Systems Analysis

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