Deleting the contents of cells by clicking on the button when pressing the command button

I am creating a spreadsheet of orders. Different product categories have options that are incompatible, so I created some checkboxes in column A. I want the control button to select the contents of each row from column H: L, which has a selected checkbox, and deletes it, and then uncheck it, I I don’t know how to write such code. Your help is really appreciated.

Sub EliminateCheckBoxes() Dim CB As CheckBox, n As Long, x As Long n = ActiveSheet.CheckBoxes.Count For x = n To 1 Step -1 Set CB = ActiveSheet.CheckBoxes(x) If CB.Name <> "Check Box 1" Then Next x End Sub 
+4
source share
1 answer

You need to use the checkbox property that refers to your corresponding string. Either (if bound) use .LinkedCell (the line then receives the range object), or if it is located in the corresponding line, then .TopLeftCell (range)

eg:.

 'Using LinkedCell Range(cb.LinkedCell).EntireRow.Range("H1:L1").Delete 'or using TopLeftCell cb.TopLeftCell.EntireRow.Range("H1:L1").Delete cb.Value = -4146 'uncheck the checkbox 

An example of your code with the above and added check if the checkbox is checked:

 Sub EliminateCheckBoxes() Dim CB As CheckBox, n As Long, x As Long n = ActiveSheet.CheckBoxes.Count For x = n To 1 Step -1 Set CB = ActiveSheet.CheckBoxes(x) If CB.Name <> "Check Box 1" Then 'are you intentionally excluding Check Box 1? If CB.Value = 1 then CB.TopLeftCell.EntireRow.Range("H1:L1").ClearContents CB.Value = -4146 'uncheck the checkbox End If End If Next x End Sub 
+3
source

Source: https://habr.com/ru/post/1496592/


All Articles