Friday, February 9, 2024

Tutorial - How To Link Multiple Checkboxes To Multiple Cells In Excel

  Tutorial - How To Link Multiple Checkboxes To Multiple Cells In Excel


Video Tutorial To Add Multiple Checkbox:


 If you want to create multiple checkboxes that linked to multiple cells, you can apply the following VBA code, and to apply code follow the below step.


1. Go to your worksheet and create checkboxes.

2. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window or you can click on Developer tab ➡ Visual Basic.


3. Click Insert > Module


4. Double click on module1 and paste the below code in the Module Window (make sure module window is clear)

Sub CreateCheckboxes()
    Dim ws As Worksheet
    Dim rnge As Range
    Dim cbx As CheckBox
    Dim cell As Range
    Dim checkBoxColumn As Integer
    Dim resultColumn As Integer
    
    On Error Resume Next ' Enable error handling
    ' Code by Excelmind
    
    ' Set the worksheet where you want to add checkboxes
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' Select the range where you want to add checkboxes
    Set rnge = Application.InputBox("Select a range", Type:=8)
    
    ' Check if the user cancelled the input box
    If rnge Is Nothing Then
        Exit Sub ' Exit the subroutine if cancelled
    End If
    
    On Error GoTo 0 ' Disable error handling
    
    ' Loop through each cell in the selected range
    For Each cell In rnge
        ' Check if the column index is odd
        If cell.Column Mod 2 = 1 Then
            ' Create a checkbox in the odd column
            Set cbx = ws.CheckBoxes.Add(cell.Left, cell.Top, cell.Width, cell.Height)
            
            ' Set the linked cell for the checkbox to the cell in the next even column
            cbx.LinkedCell = ws.Cells(cell.Row, cell.Column + 1).Address
            
            ' Adjust the checkbox appearance
            cbx.Caption = ""
            cbx.Value = xlOff
        End If
    Next cell
End Sub


4. Now click F5 to run the code or create a button and assign the macro to it.

 











1 comment:

  1. Is it possible to align the checkboxes to the center of the cells ?

    ReplyDelete

How To Use Vlookup In Merged Cells

  How To Use Vlookup In Merged Cells Click Here to download sample file for practice Note: Download the file and open with microsoft excel