Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group checkboxes as they are created

Status
Not open for further replies.

calvinb

Technical User
Jan 22, 2012
47
CA

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top