I would use the Checkboxes collection--it's less complicated.
I added some stuff that you may not need.
The linkedcell is assigned to the cell that contains the checkbox. But it's
formatted as ";;;". This means that the value in the cell doesn't appear in the
worksheet--but is still visible in the formula bar.
Using a linked cell means that you could count the number of checkboxes that are
checked with something like:
=countif(a1:a10,true)
(Remove any of those things that you don't want--especially captions.)
Option Explicit
Sub testme()
Dim CBX As CheckBox
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
For Each CBX In wks.CheckBoxes
With CBX
.LinkedCell = .TopLeftCell.Address(external:=True)
.Caption = ""
.Name = "CBX_" & .TopLeftCell.Address(0, 0)
.OnAction = "'" & ThisWorkbook.Name & "'!dothework"
.TopLeftCell.NumberFormat = ";;;"
End With
Next CBX
End Sub
Sub DoTheWork()
Dim CBX As CheckBox
Set CBX = ActiveSheet.CheckBoxes(Application.Caller)
If CBX.Value = xlOn Then
MsgBox "it's checked"
Else
MsgBox "it's not checked"
End If
'for instance...
MsgBox CBX.TopLeftCell.Address(0, 0) & vbLf & CBX.Name
End Sub
ps.
Here's a routine I've posted before that creates the checkboxes and assigns the
macros...
Option Explicit
Sub testme()
Dim myCBX As CheckBox
Dim myCell As Range
With ActiveSheet
.CheckBoxes.Delete 'nice for testing
For Each myCell In .Range("B3:B10").Cells
With myCell
Set myCBX = .Parent.CheckBoxes.Add _
(Top:=.Top, Width:=.Width, _
Left:=.Left, Height:=.Height)
With myCBX
.LinkedCell = myCell.Address(external:=True)
.Caption = ""
.Name = "CBX_" & myCell.Address(0, 0)
.OnAction = "'" & ThisWorkbook.Name & "'!dothework"
End With
.NumberFormat = ";;;"
End With
Next myCell
End With
End Sub
Sub DoTheWork()
Dim myCBX As CheckBox
Set myCBX = ActiveSheet.CheckBoxes(Application.Caller)
If myCBX = xlOn Then
'do something
Else
'do something else
End If
End Sub
ps. If you ever need a similar routine for optionbuttons from the Forms
toolbar, visit Debra Dalgleish's site:
http://contextures.com/xlForm01.html