Creating multiple checkboxes and linking them to their rows accordingly

Jakezer

New Member
Joined
Mar 24, 2022
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I have this code to create checkboxes,

Can you please help me creating multiple checkboxes in front of multiple rows (in range C), and linking each checkbox to a cell of that same row (Cells in range B), and applying a condition to change the value of that cell (which is in range C) according to its checkbox state (On or Off)

What I want is (as alogirthm or simple vba explanation to try and make it more clear maybe):

VBA Code:
[COLOR=rgb(65, 168, 95)]For Each Row in Range("A")

If Not IsEmpty(Cell(Range("C"))) Then
Cell(Range("C")).ClearContents

ElseIf IsEmpty(Cell(Range("C"))) Then

If CheckBox = True Then
Cell.Range("B").Value = 2

If CheckBox = False Then
Cell.Range("B").Value = 1

End If[/COLOR]

The Code I have:

VBA Code:
Sub AddCheckBoxes()

Dim i, LRow As Single
Dim chkbx As CheckBox
Dim MyLeft, MyTop, MyHeight, MyWidth As Double

Application.ScreenUpdating = False
LRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To LRow
    If Cells(i, "A").Value <> "" Then
        
        If Not IsEmpty(Cells(i, "C")) Then
        Cells(i, "C").ClearContents
        
        ElseIf IsEmpty(Cells(i, "C")) Then
        MyLeft = Cells(i, "C").Left
        MyTop = Cells(i, "C").Top
        MyHeight = Cells(i, "C").Height
        MyWidth = Cells(i, "C").Width
        With ActiveSheet.CheckBoxes.Add(MyLeft, MyTop, MyWidth, MyHeight)
            
            .Caption = ""
            .Value = xlOff
            .LinkedCell = "B" & LRow
            .Display3DShading = False
        End With
        End If
     End If
     
     If ActiveSheet.CheckBoxes("CheckBox1").Value = True Then
     Range("B2").Value = 2
     ElseIf ActiveSheet.CheckBoxes("CheckBox1").Value = False Then
     Range("B2").Value = 1
     End If
     
Next i

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top