如果按下命令button时单击checkbox,则删除单元格的内容

我正在构build订单configuration电子表格。 各种产品类别有不兼容的选项,所以我在列A中创build了一些checkbox。我想做一个控制button,从列H:L的每一行中select一个checkbox并删除它,然后取消selectcheckbox。 我真的不知道如何编写这样的代码。 你的帮助真的很感激。

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 

您需要使用链接到相关行的checkbox的属性。 要么(如果链接)使用.LinkedCell(string,然后获取范围对象),否则,如果位于相关的行然后.TopLeftCell(范围)

例如:

 '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 

上面的代码示例,并且检查checkbox是否被选中:

 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