There is not a straightforward way to do this. Excel VBA does not offer a way to associate the physical location of a checkbox with the cell. (It does allow you link the values, but that doesn't have anything to do with where the checkbox is actually located.)
Aside from the conceptual approach, your code has a number of problems. First, you are iterating through all cells of a Selection but the code has no way to associate a checkbox to cell cl. Second, there is no collection called CheckBoxes, and so there is no valid object on the right side of your assignment to assign to the left side. That's why you're getting the error. And even if you could do it like that, it would not work to assign it to a cb object the way that this code does.
All this code would succeed in doing is setting cb to Nothing, which does not affect the original object assigned to it. Think of an Object variable as a pointer. If you do this
Set cb = ActiveSheet.Shapes("CheckBox1")
you have set the variable cb to point to the same object identified as ActiveSheet.Shapes("CheckBox1"). If you set cb to Nothing, cb doesn't point to anything anymore but the checkbox is still alive and well and living in the active sheet.
Also,
When run through, the macro also deletes all cells on the entire page vs. just in the selected range.
this doesn't make sense if you are getting an error. The code halts as soon as it hits that line of code the first time. I don't see anything getting deleted, much less all the checkboxes on the page.
To delete a checkbox, you would need code that looks something like this:
ActiveSheet.Shapes("CheckBox1").Cut
where CheckBox1 is the name of the checkbox. That's the easy part.
The hard part is finding out what cell it's in. The only way that I know to do this is to evaluate the position of each checkbox (using the Left and Top attributes), then iterate through all the used cells on the sheet to see if the checkbox falls within the boundary of each cell (also using Top and Left attributes, and the Top and Left attributes of the cells immediately to the right and below).
Bookmarks