Add and Remove Check Boxes with VBA

Falko26

Board Regular
Joined
Oct 13, 2021
Messages
89
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have an Excel Table with an add and remove row button. Currently when these buttons are pressed the Check box is left in limbo. It doesn't copy down or get removed. Is there a way to add to my code that will add or remove the check boxes respectfully when the row is adjusted?

1651697181682.png


VBA Code:
 'Add New Row to Table Individual Labor
Sub AddRowToTable_Equip_List()
    
    'Declare Variables
    Dim oSheetName As Worksheet
    Dim sTableName As String
    Dim loTable As ListObject
    
    ' Turn off Background Noise
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    'Define Variable
    sTableName = "Equip_List"
    
    'Define WorkSheet object
    Set oSheetName = Sheets("Equip List")
    
    'Define Table Object
    Set loTable = oSheetName.ListObjects(sTableName)
    
    'Add New row to the table
    loTable.ListRows.Add
      
    ' Turn Back On Background Noise
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub

'Remove Last Row from Table Individual Labor
Sub DeleteLastRow_Equip_List()
    
    ' Turn off Background Noise
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
        Dim Tbl As ListObject
Set Tbl = ActiveSheet.ListObjects("Equip_List")

' If table is down to last row do not delete Row
    If Tbl.DataBodyRange.Rows.Count = 1 Then Exit Sub
    
'If table has data in the description column do not delete row
Range("Equip_List[Equipment_Type]").Select
Selection.Cells(Selection.Rows.Count, 1).Select
    
    If IsEmpty(Selection) = False Then
    MsgBox "Row cannot be removed when Equipment_Type column is populated.", vbExclamation
    Exit Sub
    End If

Set Tbl = Nothing

' Delete Row
    With ActiveSheet.ListObjects("Equip_List").DataBodyRange
        ans = .Rows.Count
        .Rows(ans).delete
    End With
    
    ' Turn Back On Background Noise
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Add a new row. Replace:
VBA Code:
    loTable.ListRows.Add
with:
VBA Code:
    Dim newRow As ListRow
    Set newRow = loTable.ListRows.Add
    
    'Add checkbox in new row
    
    Dim cbCell As Range
    Dim cb As CheckBox
    Set cbCell = newRow.Range.Cells(1, loTable.ListColumns("Checkbox").Index)
    Set cb = oSheetName.CheckBoxes.Add(cbCell.Left + (cbCell.Width / 2), cbCell.Top, Width:=10, Height:=10)
    With cb
        .Caption = ""
        .Value = xlOff
    End With

Delete last row. Replace:
VBA Code:
Set Tbl = Nothing

' Delete Row
    With ActiveSheet.ListObjects("Equip_List").DataBodyRange
        ans = .Rows.Count
        .Rows(ans).delete
    End With
with:
VBA Code:
    ' Delete Row
    
    Dim lastRow As Long
    Dim cb As CheckBox
    With Tbl
        lastRow = .DataBodyRange.Rows.Count
        Set cb = Find_Checkbox_In_Cell(.DataBodyRange.Cells(lastRow, .ListColumns("Checkbox").Index))
        If Not cb Is Nothing Then cb.Delete
        .DataBodyRange.Rows(lastRow).Delete
    End With
and add this function:
VBA Code:
Private Function Find_Checkbox_In_Cell(cell As Range) As CheckBox

    Dim cb As CheckBox
    
    For Each cb In cell.Worksheet.CheckBoxes
        If Not Intersect(cell, cb.TopLeftCell) Is Nothing Then
            Set Find_Checkbox_In_Cell = cb
            Exit For
        End If
    Next

End Function
Both changes assume the check box column is named "Checkbox" - change it in the above code as needed.
 
Upvote 0
Solution
Hey John,
Thanks for the Reply! Its much appreciated

The top bit of code that adds a check box when we add a row works perfectly.

The second part I'm having some troubles.

I wasn't sure where to place the Private Function so I just put it at the bottom of the page of code as shown below. Note All of this code lives in the Specific Sheet Code not a Module. Sheet Name = "WBS".

When I run the macro it makes it to this line then errors out.

Any Ideas??

Thanks again.

View attachment 64001

VBA Code:
'Remove Last Row from Table Individual Labor
Sub BLC_DeleteLastRow_WBS()

    ' Turn off Background Noise
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
        Dim Tbl As ListObject
Set Tbl = ActiveSheet.ListObjects("Table8")

' If table is down to last row do not delete Row
    If Tbl.DataBodyRange.Rows.Count = 1 Then Exit Sub
    
'If table has data in the description column do not delete row
Range("Table8[WBS]").Select
Selection.Cells(Selection.Rows.Count, 1).Select
    
    If IsEmpty(Selection) = False Then
    MsgBox "Row cannot be removed when Description column is populated.", vbExclamation
    Exit Sub
    End If

Set Tbl = Nothing

    ' Delete Row
    Dim lastRow As Long
    Dim cb As CheckBox
    With Tbl
        lastRow = .DataBodyRange.Rows.Count
        Set cb = Find_Checkbox_In_Cell(.DataBodyRange.Cells(lastRow, .ListColumns("Checkbox").Index))
        If Not cb Is Nothing Then cb.Delete
        .DataBodyRange.Rows(lastRow).Delete
    End With
    
    ' Turn Back On Background Noise
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub

Private Function Find_Checkbox_In_Cell(cell As Range) As CheckBox

    Dim cb As CheckBox
    
    For Each cb In cell.Worksheet.CheckBoxes
        If Not Intersect(cell, cb.TopLeftCell) Is Nothing Then
            Set Find_Checkbox_In_Cell = cb
            Exit For
        End If
    Next

End Function
 
Upvote 0
John,

Ignore the above post. I figured it out, I still had the line of code: Set Tbl = Nothing on there so that's why I was getting the error.

Your original Post was Spot on!! Thanks again This has been a huge help!
 
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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