Excel VBA脚本清除列中的所有checkbox

我试图清除特定范围的单元格中的所有活动的Xcheckbox,或按列

这看起来很简单,因为我可以用下面的代码完成整个工作表。 我只是不知道如何修改这只适用于单列或单元格的范围。

Sub ClearAllCheckboxes() Dim Answer As String Dim MyNote As String Dim Obj As Object MyNote = "This will Clear all CheckBoxes Proceed? " Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???") If Answer = vbNo Then Exit Sub Else On Error Resume Next For Each Obj In ActiveSheet.OLEObjects Obj.Object.Value = False Next Obj End If End Sub 

尝试使用topleftcell属性:

 Sub ClearAllCheckboxes() Dim Answer As String Dim MyNote As String Dim Obj As Object dim rg as range set rg=selection MyNote = "This will Clear all CheckBoxes Proceed? " Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???") If Answer = vbNo Then Exit Sub Else On Error Resume Next For Each Obj In ActiveSheet.OLEObjects if not intersect(rg, obj.topleftcell) is nothing then _ Obj.Object.Value = False Next Obj End If End Sub 
 Obj.TopLeftCell.Column 

会告诉你这个checkbox位于哪一列。基于此,你可以决定是否要改变它的值。