I have working code to create 3 groups of checkboxes as shown below. I would like to be able to create mutually exclusive groups as the checkboxes are being created.
Code:
Private Sub CreateCheckboxes_Click()
' deletes all checkboxes and then creates new ones centered in the column and row
Dim myCell As Range
Dim myRng As Range
Dim CBX As CheckBox
'------------------------------------first group of checkboxes-----------------------------------------------------
With ActiveSheet
.CheckBoxes.Delete ' deletes all checkboxes in the activesheet so be careful!
Set myRng = .Range("I4:K23") ' the .Range setting is where the checkboxes will be created
End With
For Each myCell In myRng.Cells
With myCell
Set CBX = .Parent.CheckBoxes.Add( _
Top:=.Top, _
Left:=.Left, _
Width:=1, _
Height:=1)
CBX.Name = "CBX_" & .Address(0, 0)
CBX.Caption = ""
CBX.Left = .Left + ((.Width - CBX.Width) / 2)
CBX.Top = .Top + ((.Height - CBX.Height) / 2)
CBX.LinkedCell = .Offset(0, 14).Address(external:=True) ' Offset(X, B) where X is the linked cell Row and B is the linked cell Column
' so in this example (0, 14) the linked cell is on the same row but 14 columns to the right of the checkbox
CBX.Value = xlOff
.Offset(0, 1).NumberFormat = ";;;"
End With
Next myCell
'---------------------------------------------------- second group of checkboxes--------------------------------------------------
With ActiveSheet
' .CheckBoxes.Delete ' deletes all checkboxes in the activesheet so be careful!
Set myRng = .Range("L4:M23") ' the .Range setting is where the checkboxes will be created
End With
For Each myCell In myRng.Cells
With myCell
Set CBX = .Parent.CheckBoxes.Add( _
Top:=.Top, _
Left:=.Left, _
Width:=1, _
Height:=1)
CBX.Name = "CBX_" & .Address(0, 0)
CBX.Caption = ""
CBX.Left = .Left + ((.Width - CBX.Width) / 2)
CBX.Top = .Top + ((.Height - CBX.Height) / 2)
CBX.LinkedCell = .Offset(0, 14).Address(external:=True) ' Offset(X, B) where X is the linked cell Row and B is the linked cell Column
' so in this example (0, 14) the linked cell is on the same row but 14 columns to the right of the checkbox
CBX.Value = xlOff
.Offset(0, 1).NumberFormat = ";;;"
End With
Next myCell
'---------------------------------------------------- third group of checkboxes--------------------------------------------------
With ActiveSheet
' .CheckBoxes.Delete ' deletes all checkboxes in the activesheet so be careful!
Set myRng = .Range("N4:P23") ' the .Range setting is where the checkboxes will be created
End With
For Each myCell In myRng.Cells
With myCell
Set CBX = .Parent.CheckBoxes.Add( _
Top:=.Top, _
Left:=.Left, _
Width:=1, _
Height:=1)
CBX.Name = "CBX_" & .Address(0, 0)
CBX.Caption = ""
CBX.Left = .Left + ((.Width - CBX.Width) / 2)
CBX.Top = .Top + ((.Height - CBX.Height) / 2)
CBX.LinkedCell = .Offset(0, 14).Address(external:=True) ' Offset(X, B) where X is the linked cell Row and B is the linked cell Column
' so in this example (0, 14) the linked cell is on the same row but 14 columns to the right of the checkbox
CBX.Value = xlOff
'.Offset(0, 1).NumberFormat = ";;;"
End With
Next myCell
End Sub