Add Checkbox with new table row

vanwooten

Board Regular
Joined
Dec 15, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
I have a table where the first row contains a checkbox:

1710700908049.png

I use the following code to add rows to this table:

Set newrow = WS.ListObjects("Trades").ListRows.Add

With newrow
.Range(1) = Date
.Range(3) = "0930"
.Range(5) = "=IFS($J$3=""Eastern"",VLOOKUP([@[Etime Convert]],Sheet1!A$2:B$23,2,TRUE),$J$3=""Central"",VLOOKUP([@[Etime Convert]],Sheet1!C$2:D$23,2,TRUE),$J$3=""Mountain"",VLOOKUP([@[Etime Convert]],Sheet1!E$2:F$23,2,TRUE),$J$3=""Pacific"",VLOOKUP([@[Etime Convert]],Sheet1!G$2:H$23,2,TRUE))"
.Range(6) = WS.Range("I5")
.Range(7) = WS.Range("K5")
.Range(8) = WS.Range("J5")
.Range(9) = 0
.Range(10) = 0
.Range(11) = WS.Range("L7")
.Range(18) = 0
End With

As you can see in the image, I am not getting checkboxes on new rows and I would like to have them with the same formatting and the linked cell moving to the new row. Any Ideas?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You don't actually say which column should contain the checkbox and which cell should be the linked cell. Assuming column 2 of the table and the linked cell is the same cell that the checkbox sits in:

VBA Code:
    Dim cbShape As Shape
    Dim cb As CheckBox
   
    Set newRow = WS.ListObjects("Trades").ListRows.Add
   
    With newRow
        Set cbShape = WS.Shapes.AddFormControl(xlCheckBox, .Range(2).Left, .Range(2).Top - 1, 15, 15)
        With cbShape
            .Name = "cb_" & newRow.Index
            .TextFrame.Characters.Text = ""
            Set cb = .OLEFormat.Object
        End With
        cb.LinkedCell = .Range(2).Address
        ':
        ':
    End With

Or shorter:
VBA Code:
    Dim cb As CheckBox
    Set newRow = WS.ListObjects("Trades").ListRows.Add
   
    With newRow
        Set cb = WS.CheckBoxes.Add(.Range(2).Left, .Range(2).Top - 1, 15, 15)
        With cb
            .Caption = ""
            .LinkedCell = newRow.Range(2).Address
        End With
        ':
        ':
    End With
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,223,947
Messages
6,175,562
Members
452,652
Latest member
eduedu

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