VBA: Delete checkboxes in cell range

FunsizedNerd

New Member
Joined
Mar 20, 2019
Messages
17
I am trying to set up a macro that will search the range of cells for any checkboxes, and if there are any in the range, they will be deleted (and then I add new ones in the next step).

However, when I run the macro, it deletes ALL checkboxes in the sheet, rather than just those that are within the selected range. How can I change the If statement and Intersect condition to work properly?


Dim c As Range, myRange As Range
Dim check As CheckBox

'OD Checkboxes
Range("F2", Range("F2").End(xlDown)).Select
Selection.Offset(0, -1).Select
Set myRange = Selection
For Each check In Sheets("Summary").CheckBoxes
If Not Intersect(check.TopLeftCell, Range("myRange")) Is Nothing Then
check.Delete
End If
Next
 

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.
Try...

Code:
    Dim ws As Worksheet
    Dim myRange As Range
    Dim check As CheckBox
    
    Set ws = Sheets("Summary")
    
    'OD Checkboxes
    Set myRange = ws.Range("F2", ws.Range("F2").End(xlDown)).Offset(, -1)
    
    For Each check In ws.CheckBoxes
        If Not Intersect(check.TopLeftCell, myRange) Is Nothing Then
            check.Delete
        End If
    Next

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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