Reputation: 4527
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:
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?
Upvotes: 0
Views: 793
Reputation: 11
I found this very helpful. I turned the code around so that I could specify a large number of cells as tick boxethat could each be ticked while maintaining the underlying values:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim TickBoxes As Range
'Specify the range of cells you want to be tickboxes
Set TickBoxes = Union(Range("A1"), Range("B2:D8"), Range("E9:G15")) 'etc
If Intersect(TickBoxes, Target) Is Nothing Then Exit Sub
Target.Font.Name = "Marlett"
Cancel = True
If Target.Value = 1 Then
Target.Value = 0
Target.NumberFormat = ";;;"
Else
Target.Value = 1
Target.NumberFormat = Chr(34) & "a" & Chr(34) & ";;;"
End If
End Sub
Not that clever, but hopefully of use to someone!
Alan K
Upvotes: 1
Reputation: 96791
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:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim B9 As Range
Set B9 = Range("B9")
If Intersect(B9, Target) Is Nothing Then Exit Sub
B9.Font.Name = "Marlett"
Cancel = True
If B9.Value = 1 Then
B9.Value = 0
B9.NumberFormat = ";;;"
Else
B9.Value = 1
B9.NumberFormat = Chr(34) & "a" & Chr(34) & ";;;"
End If
End Sub
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.
Upvotes: 2