vba to remove checkboxes

kashman

New Member
Joined
Sep 15, 2016
Messages
4
Hello,
I am using the code below to add check boxes to column D when column C is populated. I would like these check boxes to disappear when cells in column C are blank again. Although the check boxes appear I am unable to make them disappear when column C goes blank. Please help what I am doing wrong.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ToRow As Long
    Dim LastRow As Long
    Dim MyLeft As Double
    Dim MyTop As Double
    Dim MyHeight As Double
    Dim MyWidth As Double
    '--------------------------
    LastRow = Range("C65536").End(xlUp).Row
    For ToRow = 11 To LastRow
        If Not IsEmpty(Cells(ToRow, "C")) Then
            MyLeft = Cells(ToRow, "D").Left
            MyTop = Cells(ToRow, "D").Top
            MyHeight = Cells(ToRow, "D").Height
            MyWidth = MyHeight = Cells(ToRow, "D").Width
            
            ActiveSheet.CheckBoxes.Add(MyLeft, MyTop, MyWidth, MyHeight).Select
             With Selection
                .Caption = ""
                .Value = xlOff
                .Display3DShading = False
            End With
        End If
        If IsEmpty(Cells(ToRow, "C")) Then
        ActiveSheet.CheckBoxes.Delete
        Selection.FormatConditions.Delete
    End If
    Next
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oName As Name
    Dim oCell As Range
    
    For Each oCell In Target.Cells
        If oCell.Column = 3 And oCell.Row >= 11 Then
            On Error Resume Next
            With oCell.Offset(0, 1)
                If Not IsEmpty(oCell) Then
                    Set oName = .Name
                    If Err.Number <> 0 Then
                        With CheckBoxes.Add(.Left, .Top, .Width, .Height)
                            .Caption = ""
                            .Value = xlOff
                            .Display3DShading = False
                            oCell.Offset(0, 1).Name = Replace(.ShapeRange.Name, " ", "_")
                            oCell.Offset(0, 1).Name.Visible = False
                        End With
                    End If
                Else
                    CheckBoxes(Replace(.Name.Name, "_", " ")).Delete
                    .Name.Delete
                End If
            End With
            On Error GoTo 0
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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