2012-11-05 102 views
4

所有複選框我試圖清除細胞中的特定範圍內的所有活動的X複選框,或者列Excel的VBA腳本清楚

看起來這將是簡單的,因爲我是能夠做到這一點爲整個工作表使用下面的代碼。我只是不確定如何修改這隻適用於單列或單元格範圍。

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 

回答

1

嘗試使用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 
+1

完美適用於現有的程序 非常感謝:) –

+1

@SteveHobbs作爲一個新用戶,請注意,你可以「接受」這個答案從nutsch。這將爲你(2)和nutch(15)提供聲望點, – brettdj

1
Obj.TopLeftCell.Column 

會告訴你該複選框位於哪一列基礎上,你可以決定是否要改變其值或不。