+ Reply to Thread
Results 1 to 2 of 2

Macro to Delete All Checkboxes in Selected Cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2012
    Location
    Westport, CT
    MS-Off Ver
    Excel 2010
    Posts
    3

    Macro to Delete All Checkboxes in Selected Cells

    I'm trying to program a simple macro to delete all checkboxes in cells that are selected. I am modifying a script that VertexVortex wrote for me to add checkboxes to selected cells. The script I am using (for the deletion) is as follows:

    Sub CheckboxRemove()
    Dim cl As Range
    Dim cb As Object
    
    For Each cl In Selection
    Set cb = ActiveSheet.CheckBoxes.Delete()
    Next cl
    
    Set cl = Nothing
    Set cb = Nothing
    End Sub
    I am getting an error saying "Object Required" on the line 'set cb = ActiveSheet.Checkboxes.Delete'. When run through, the macro also deletes all cells on the entire page vs. just in the selected range. Any help would be greatly appreciated, thanks!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,729

    Re: Macro to Delete All Checkboxes in Selected Cells

    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).
    Last edited by 6StringJazzer; 10-22-2012 at 06:14 PM. Reason: Added further explanation
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1