vba 如何删除一系列单元格中的所有复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15509820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:07:53  来源:igfitidea点击:

How do delete all checkboxes in a range of cells

excelexcel-vbavba

提问by Jamie Walker

I have code to delete a checkbox in a certain cell but I need it to delete all checkboxes in a range I have selected. Following is the code I have that deletes a checkbox in a certain cell.

我有代码可以删除某个单元格中的复选框,但我需要它来删除我选择的范围内的所有复选框。以下是我删除某个单元格中的复选框的代码。

Columns("B:B").Select
Selection.Find(What:="FIELD SERVICES", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, -1).Select
Dim CB8 As CheckBox
For Each CB8 In ActiveSheet.CheckBoxes
    If CB8.TopLeftCell.Address = ActiveCell.Address Then CB8.Delete
    Next

Following is how I tried to alter it to delete cells in the range I need but it only deletes the checkbox in the first cell of the range.

以下是我如何尝试更改它以删除我需要的范围内的单元格,但它只删除范围内第一个单元格中的复选框。

Columns("B:B").Select
Selection.Find(What:="FIELD SERVICES", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range(ActiveCell.Offset(1, -1), ActiveCell.Offset(8, 0).Select
Dim CB8 As CheckBox
For Each CB8 In ActiveSheet.CheckBoxes
    If CB8.TopLeftCell.Address = ActiveCell.Address Then CB8.Delete
    Next

Any advice is greatly appreciated.

任何意见是极大的赞赏。

回答by Tim Williams

Dim f as Range, cbRange as range
Dim CB8 As CheckBox


Set f = Columns("B:B").Find(What:="FIELD SERVICES", After:=ActiveCell, _
                            LookIn:=xlFormulas, LookAt:=xlPart)

if not f is Nothing then
   set cbRange = f.parent.range(f.Offset(1, -1), f.Offset(8, 0))
   For Each CB8 In ActiveSheet.CheckBoxes
     If not application.intersect(CB8.TopLeftCell, cbRange) is nothing Then CB8.Delete
   Next
end if