Clear Entire Row if Checkbox Selected

SarahTheBookDragon

New Member
Joined
Mar 19, 2024
Messages
2
Office Version
  1. 365
Howdy!
I have an excel sheet that has a table spanning from Columns A-L.
I have a row of checkboxes in Column M
The checkboxes have conditional formatting that grays/strikes out the corresponding row that it is associated with

However- I want to update this so that it clears the row entirely.
Please note: I only want the row to be clear. I do not want the row to be deleted (my hope is to continue to use the same worksheet without having to re-add the checkboxes) .

I found a VBA code here that I have used with some success:

Public Sub CheckBox()
If Range("M18") = True Then
Range("A18:L18").ClearContents
End If

However- I want to apply this to all the checkboxes in Column M.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Have a Try. My macro, as it is, will test all the checkboxes in the sheet, so if you have other checkboxes throughout the sheet other than column M, you'll need to narrow down the range being tested (as homework ;)).
VBA Code:
Option Explicit
Public Sub CheckBox()
    Dim ChkBox As Object
    Dim CellRow As String
    For Each ChkBox In ActiveSheet.CheckBoxes       'loop all through checkboxes
        If ChkBox.Value = 1 Then                    'detect if checkbox is checked
            CellRow = ChkBox.TopLeftCell.Row        'detect checked checkbox row
            Range("A" & CellRow & ":L" & CellRow).ClearContents 'clear cells
            ChkBox.Value = 0                        'reset checkbox
        End If
    Next ChkBox
End Sub
 
Upvote 0
Possible alternative - only checks checkboxes that change in column M. Change sheet name to suit.

VBA Code:
Sub Change_Check_Boxes()
    Dim c As Excel.CheckBox
    Set c = Worksheets("Sheet1").CheckBoxes(Application.Caller) '<-- *** Change sheet name to suit ***
    If c.TopLeftCell.Column = 13 Then
        If c.Value = xlOn Then Range(Cells(c.TopLeftCell.Row, 1), Cells(c.TopLeftCell.Row, 12)).ClearContents
    End If
End Sub
 
Upvote 0
Have a Try. My macro, as it is, will test all the checkboxes in the sheet, so if you have other checkboxes throughout the sheet other than column M, you'll need to narrow down the range being tested (as homework ;)).
VBA Code:
Option Explicit
Public Sub CheckBox()
    Dim ChkBox As Object
    Dim CellRow As String
    For Each ChkBox In ActiveSheet.CheckBoxes       'loop all through checkboxes
        If ChkBox.Value = 1 Then                    'detect if checkbox is checked
            CellRow = ChkBox.TopLeftCell.Row        'detect checked checkbox row
            Range("A" & CellRow & ":L" & CellRow).ClearContents 'clear cells
            ChkBox.Value = 0                        'reset checkbox
        End If
    Next ChkBox
End Sub
Thank you for your reply :) - this definitely did something... but did not work as i hoped :(
It cleared other rows with checkboxes (but not the UNSELECTED ones)
 
Upvote 0
It cleared other rows with checkboxes (but not the UNSELECTED ones)
At least it clears out only selected checkboxes ;).
In the absence of a sample file, since we don't have a crystal ball, you'd mind explaining exactly what your hope was and whether, as I pointed out, you have any other checkboxes throughout your sheet besides the ones in column M.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
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