Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro to delete a checkbox from a certain cell

Tags:

excel

vba

I am pulling check boxes into a spreadsheet to be used to select certain line items to get a final cost. There are a few unneeded check boxes that get pulled in though, probably 5 or so total. I can use macros to get to the specific cells these unneeded check boxes. These unneeded check boxes will not always be in the same place due to my data changing so I will have to delete them one at a time which shouldn't be a problem other than I don't know the code to delete a check box from the active cell. I need a code to delete check box from active cell or selected cell. I have included some of my coding I have tried below. The first section is just getting me to the correct cell to delete the check box out of. The second part are two different codes I have tried to delete the check box but neither worked. I appreciate your input.

'To delete unwanted checkboxes
    Sheets("Quote Sheet").Select
    Range("B9").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, -1).Select

    ActiveSheet.Shapes.Range(Array("Check Box 456")).Select
    Selection.Delete
    Selection.Cut

    ActiveCell.CheckBoxes.Delete
    Selection.FormatConditions.Delete
like image 563
Jamie Walker Avatar asked Jan 15 '23 04:01

Jamie Walker


1 Answers

This code will delete any Excel checkbox located at the active cell.

Sub DeleteCheckbox()
    Dim cb As CheckBox

    For Each cb In ActiveSheet.CheckBoxes
        If cb.TopLeftCell.Address = ActiveCell.Address Then cb.Delete
    Next
End Sub

In case you're using ActiveX checkboxes, this code will do the job:

Sub DeleteActiveXCheckbox()
    Dim obj As OLEObject
    Dim cb As MSForms.CheckBox

    For Each obj In ActiveSheet.OLEObjects
        If TypeOf obj.Object Is MSForms.CheckBox Then
            Set cb = obj.Object
            If cb.ShapeRange.Item(1).TopLeftCell.Address = _
                ActiveCell.Address Then obj.Delete
        End If
    Next
End Sub
like image 185
Peter Albert Avatar answered Jan 31 '23 06:01

Peter Albert