Creating checkbox with VBA for each new table row

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
97
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello excel experts.

I'm trying to create a checkbox in column B, linked to column C, for each new row that I create in a table.

I have this VBA code that I'm testing and its a mix of recorded macro and code found on the internet:

VBA Code:
Sub ButtonClick10()
Dim lastRow As Long
    Dim sh As Worksheet
  
    Set sh = Sheets("Salarios")
  
    With sh
        lastRow = .Cells(Rows.Count, "D").End(xlUp).Row
    End With
  
    With sh.Range("B" & lastRow)
        With sh.CheckBoxes.Add(20.25, 147, 72, 72)
            .Caption = ""
            .Locked = False
            .LockedText = False
            .Value = xlOff
            .LinkedCell = Range("C", lastRow)
        End With
    End With
End Sub

this line:
.LinkedCell = Range("C", lastRow) is givin an error.

This code is working with a button just to test purposes, because the goal is to have it working every time I add a row to the table.

This code is creating the cell in the wrong place and not linked to the cell in column C.

How do I make this code work every time a row is added to that table?
How to properly create the checkbox in the right place?
And will it delete that checkbox if I delete a row?
In the print screen that I attached, I should have the checkbox in row 11, but it was created in row 12, with no cell linked.


Thanks to every one
 

Attachments

  • Captura de ecrã 2022-11-07 113023.png
    Captura de ecrã 2022-11-07 113023.png
    8.3 KB · Views: 43

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
VBA Code:
Sub ButtonClick10()
Dim lastRow As Long
Dim sh As Worksheet
Dim worksheet1 As String: worksheet1 = "Sheet1" 'Salarios
Dim PagoColumn As String: PagoColumn = "J"
Dim StatusColumn As String: StatusColumn = "k"
Dim LastRowColumn As String: LastRowColumn = "l:l" 'Include Entire Column.
 
    Set sh = Sheets(worksheet1)
 
    With sh
        lastRow = WorksheetFunction.CountA(Range(LastRowColumn))
    End With
 
    With sh.Range(PagoColumn & lastRow)
        With sh.CheckBoxes.Add(Cells(lastRow, PagoColumn).Left, Cells(lastRow, PagoColumn).Top, 10, 10)
            .Caption = ""
            .Locked = False
            .LockedText = False
            .Value = xlOff
            .LinkedCell = Cells(lastRow, StatusColumn).Address
        End With
    End With
End Sub

Tell me if this works for you! There are some variables that you might need to change.
 
Upvote 0
VBA Code:
Sub ButtonClick10()
Dim lastRow As Long
Dim sh As Worksheet
Dim worksheet1 As String: worksheet1 = "Sheet1" 'Salarios
Dim PagoColumn As String: PagoColumn = "J"
Dim StatusColumn As String: StatusColumn = "k"
Dim LastRowColumn As String: LastRowColumn = "l:l" 'Include Entire Column.
 
    Set sh = Sheets(worksheet1)
 
    With sh
        lastRow = WorksheetFunction.CountA(Range(LastRowColumn))
    End With
 
    With sh.Range(PagoColumn & lastRow)
        With sh.CheckBoxes.Add(Cells(lastRow, PagoColumn).Left, Cells(lastRow, PagoColumn).Top, 10, 10)
            .Caption = ""
            .Locked = False
            .LockedText = False
            .Value = xlOff
            .LinkedCell = Cells(lastRow, StatusColumn).Address
        End With
    End With
End Sub

Tell me if this works for you! There are some variables that you might need to change.
Hello @ZMyrrh, thanks for your help

The checkbox was created, but it was placed in the wrong row.
I changed the variables, to work with my file
 

Attachments

  • Captura de ecrã 2022-11-08 090411.png
    Captura de ecrã 2022-11-08 090411.png
    12.7 KB · Views: 60
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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