Automatically Add and Delete Checkboxes

SaraWitch

Active Member
Joined
Sep 29, 2015
Messages
389
Office Version
  1. 365
Platform
  1. Windows
Hello peeps.

I have a waiting list where I want a checkbox to appear in column V if data is entered in column C, from row 3. I have a code that (kind of!) works (I just have to manually add a checkbox to V3). But the code's not working to delete the checkbox when the row is cut or deleted (as well as row deletion, I want functionality to be able to move cancelled/completed applications to another sheet).

¦ MrExcel Queries.xlsm
BCDEFGHIJKLMNOPQRSTUV
3testActive FALSE
4testActive 
5testActive 
6testActive 
LEC Current Waiting List (2)
Cell Formulas
RangeFormula
D3:D6D3=IF(C3="","",IF(NOT(ISBLANK(U3)),"Complete",IF(V3=TRUE,"Cancelled/Removed",IF(R3="Offered","Offer in progress",IF(R3="Accepted","Offer accepted",IF(S3=1,"One refused offer",IF(S3=2,"Two refused offers",IF(S3=3,"Three refused offers", "Active"))))))))
J3:J6J3=IF(K3="","",IF(ISNUMBER(MATCH(K3,'[LEC Garage Waiting List.xlsm]Lookups'!$E$4:$E$12, 0)), "Eastbourne Borough Council", "Lewes District Council"))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
V3:V1048576Expression=$V3="TRUE"textNO
B3:V6Expression=$D3="Cancelled"textNO
B3:V6Expression=$D3="Complete"textNO
B3:V6Expression=$D3="Three refused offers"textNO
B3:V6Expression=$D3="Two refused offers"textNO
B3:V6Expression=$D3="One refused offer"textNO
B3:V6Expression=$D3="Offer in progress"textNO
Cells with Data Validation
CellAllowCriteria
E3:E6List=Priority
R3:R1048576List=OfferStatus
S3:S1048576List=NoOfOffers
U3:U6Datebetween 01/01/1900 and 31/12/3000
I3:I6Datebetween 01/01/1900 and 31/12/3000
N3List=YesNo
K3:L6List=Area
M3:M6List=IntendedUse
N4:O1048576List=Area
P4:P1048576List=IntendedUse
Q4:Q1048576List=YesNo


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim chkBox As CheckBox
    Dim cmToPoints As Double
    Dim chkWidth As Double, chkHeight As Double
    Dim lastRow As Long
    Dim rowNum As Long
    Dim cell As Range
    Dim checkboxExists As Boolean

    ' Set the worksheet to the active sheet
    Set ws = Me
    cmToPoints = 28.35  ' Conversion factor from cm to points

    ' Define the size of the checkbox (in points)
    chkWidth = 0.6 * cmToPoints   ' 0.6 cm in points
    chkHeight = 0.59 * cmToPoints ' 0.59 cm in points

    ' Determine the last row with data in column C
    lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

    ' Check if the change is within the range of column C (where new data is entered)
    If Not Intersect(Target, ws.Range("C3:C" & lastRow)) Is Nothing Then
        ' Loop through each cell in the changed range
        For Each cell In Intersect(Target, ws.Range("C3:C" & lastRow))
            rowNum = cell.Row
            checkboxExists = False
            
            ' Check if there's already a checkbox in the current row in column V
            For Each chkBox In ws.CheckBoxes
                If chkBox.TopLeftCell.Row = rowNum Then
                    checkboxExists = True
                    Exit For
                End If
            Next chkBox
            
            ' Only add a checkbox if one does not already exist
            If Not checkboxExists Then
                ' Add the checkbox to the current row in column V
                Set chkBox = ws.CheckBoxes.Add(Top:=ws.Cells(rowNum, "V").Top, _
                                               Left:=ws.Cells(rowNum, "V").Left, _
                                               Width:=chkWidth, _
                                               Height:=chkHeight)
                
                ' Link the checkbox to the same cell in column V
                chkBox.LinkedCell = ws.Cells(rowNum, "V").Address
                
                ' Optional: Set the checkbox to default to uncheck
                chkBox.Value = xlOff
                
                ' Remove the text from the checkbox (this clears the label in the cell)
                chkBox.Text = ""
                
                ' Center the checkbox horizontally and vertically within the cell
                chkBox.Top = ws.Cells(rowNum, "V").Top + (ws.Cells(rowNum, "V").Height - chkHeight) / 2
                chkBox.Left = ws.Cells(rowNum, "V").Left + (ws.Cells(rowNum, "V").Width - chkWidth) / 2
            End If
        Next cell
    End If

    ' Check if the data in column C is cleared (row deleted or cut)
    If Not Intersect(Target, Me.Range("C3:C" & lastRow)) Is Nothing Then
        For Each cell In Intersect(Target, Me.Range("C3:C" & lastRow))
            If IsEmpty(cell.Value) Then
                ' Row is cleared, now delete the checkbox from the corresponding row in column V
                For Each chkBox In ws.CheckBoxes
                    If chkBox.TopLeftCell.Row = cell.Row Then
                        chkBox.Delete
                    End If
                Next chkBox
            End If
        Next cell
    End If
End Sub

Any help would be appreciated! :)
 
Do you really need Checkbox controls now that there are checkboxes in cell in O365?
I would see the point if you attach some macro to them, otherwise, if it is just about TRUE/FALSE I suggest you use the Checkbox formatted cell (Insert Tab on the Ribbon).
 
Upvote 0
Oh my! I really am overthinking it!! This is much easier!! Thank you!

Is there a way to automatically add to a new row in the table though?
 
Upvote 0
to add:
VBA Code:
    Range("V10").CellControl.SetCheckbox
to remove:
VBA Code:
    Range("V10").ClearContents RemoveControls:=True
'or
    Range("V10").Clear
 
Upvote 0
Sorry, Bob; I'm still inexperienced with coding, so am unsure how the whole code would look...?
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowNum As Long
    Dim cell As Range
    Dim cv As Range
    ' Set the worksheet to the active sheet
    Set ws = Me

    ' Determine the last row with data in column C
    lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

    ' Check if the change is within the range of column C (where new data is entered)
    If Not Intersect(Target, ws.Range("C3:C" & lastRow)) Is Nothing Then
        ' Loop through each cell in the changed range
        For Each cell In Intersect(Target, ws.Range("C3:C" & lastRow))
            rowNum = cell.Row
            Set cv = ws.Range("V" & rowNum)
            ' Check if the data in column C is cleared (row deleted or cut)
            If IsEmpty(cell.Value) Then
                ' Row is cleared, now delete the checkbox from the corresponding row in column V
                cv.ClearContents True
            Else
                ' Check if there's already a checkbox in the current row in column V
                If cv.CellControl.Type <> 2 Then 'xlTypeCheckbox
                    ' Only add a checkbox if one does not already exist
                    cv.CellControl.SetCheckbox
                End If
            End If
        Next cell
    End If
End Sub
 
Upvote 0
Hi Bob,

I have tried amending the code to include adding checkboxes to (new) columns H and I (and existing column (now X)) but am getting tied up in knots. Even tried the AI route to amend your code but none were working!

New example:
¦ MrExcel Queries.xlsm
BCDEFGHIJKLMNOPQRSTUVWX
2Cx IDNameStatusPriorityAddressExisting tenantOut of areaTelephoneE-mailApplication DateCompanyFirst Choice AreaSecond Choice AreaIntended UseActive Garage AgreementNotesOffer - Garage AddressCx Garage Asset IDOffer StatusNumber of OffersCx Agreement Ref.Agreement Start DateCancelled
3testActive 
4testActive 
Auto checkbox
Cell Formulas
RangeFormula
D3:D4D3=IF(C3="","",IF(NOT(ISBLANK(W3)),"Complete",IF(X3=TRUE,"Cancelled/Removed",IF(T3="Offered","Offer in progress",IF(T3="Accepted","Offer accepted",IF(U3=1,"One refused offer",IF(U3=2,"Two refused offers",IF(U3=3,"Three refused offers", "Active"))))))))
L3:L4L3=IF(M3="","",IF(ISNUMBER(MATCH(M3,'[LEC Garage Waiting List.xlsm]Lookups'!$E$4:$E$12, 0)), "Eastbourne Borough Council", "Lewes District Council"))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
X3:X1048576Expression=$X3="TRUE"textNO
B3:X4Expression=$D3="Cancelled"textNO
B3:X4Expression=$D3="Complete"textNO
B3:X4Expression=$D3="Three refused offers"textNO
B3:X4Expression=$D3="Two refused offers"textNO
B3:X4Expression=$D3="One refused offer"textNO
B3:X4Expression=$D3="Offer in progress"textNO
Cells with Data Validation
CellAllowCriteria
T3:T1048576List=OfferStatus
U3:U1048576List=NoOfOffers
E3:E4List=Priority
P3List=YesNo
M3:N4List=Area
O3:O4List=IntendedUse
P4:Q1048576List=Area
R4:R1048576List=IntendedUse
S4:S1048576List=YesNo
W3:W4Datebetween 01/01/1900 and 31/12/3000
K3:K4Datebetween 01/01/1900 and 31/12/3000


Any further help would be greatly appreciated :)
 
Upvote 0
Column I (Telephone) does not look suitable for a checkbox. Do you mean G and H?
Are the conditions for a check box the same as before?
I am not sure I completely understand the new requirements but try this and can be adjusted further if needed:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowNum As Long
    Dim cell As Range
    Dim cv As Range
    ' Set the worksheet to the active sheet
    Set ws = Me

    ' Determine the last row with data in column C
    lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

    ' Check if the change is within the range of column C (where new data is entered)
    If Not Intersect(Target, ws.Range("C3:C" & lastRow)) Is Nothing Then
        ' Loop through each cell in the changed range
        With ws
            For Each cell In Intersect(Target, .Range("C3:C" & lastRow))
                rowNum = cell.Row
               
                ' Check if the data in column C is cleared (row deleted or cut)
                If IsEmpty(cell.Value) Then
                    ' Row is cleared, now delete the checkbox from the corresponding row in column V
                    .Range("X" & rowNum).ClearContents True
                    .Range("H" & rowNum).ClearContents True
                    .Range("G" & rowNum).ClearContents True
                Else
                    ' Check if there's already a checkbox in the current row in column V
                    If .Range("X" & rowNum).CellControl.Type <> 2 Then 'xlTypeCheckbox
                        ' Only add a checkbox if one does not already exist
                        .Range("X" & rowNum).CellControl.SetCheckbox
                    End If
                    If .Range("H" & rowNum).CellControl.Type <> 2 Then 'xlTypeCheckbox
                        ' Only add a checkbox if one does not already exist
                        .Range("H" & rowNum).CellControl.SetCheckbox
                    End If
                    If .Range("G" & rowNum).CellControl.Type <> 2 Then 'xlTypeCheckbox
                        ' Only add a checkbox if one does not already exist
                        .Range("G" & rowNum).CellControl.SetCheckbox
                    End If
                End If
            Next cell
        End With
    End If
End Sub
 
Upvote 0
Solution

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