I'm developing an Excel addin that will acquire data from a webservice before constructing a workbook.
In the example workbook I've been given as part of the requirement some of the cells are 1 or empty.
These are SUM
med and the result stored elsewhere.
I don't especially like this and I'm trying to think of a way to use checkboxes for these fields but still keep the cell values as 1 or 0 (for checked and unchecked).
This gives me a few questions:
- Can a checkbox be added and configured so that checking it gives the value 1 and unchecking it gives 0 to the associated cell?
- Can this be done programmatically from within an Excel addin?
Using my extensive research capabilities (Google) I found that I can add a checkbox and associate it with a cell so that checking/unchecking sets the cell value to TRUE
or FALSE
. If this is the only option I could probably deal with it (sigh!) but can this be done from c# code in an addin?
This is an example using cell B9
It is implemented using Double-click rather than Single-click
Enter the following event macro in the worksheet code area:
Double-clicking B9 will toggle the check.
Because it is worksheet code, it is very easy to install and automatic to use:
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
To remove the macro:
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
NOTE:
It is possible to implement an event using the standard left button click, but it is a bit more code.