Excel VBA表单复选框

时间:2013-10-15 21:57:47

标签: excel vba checkbox

嘿,我是Excel VBA的新手,需要别人的帮助。以下是我要找的:

如果选中某个未链接(表单)复选框(topleftcell?)则    它下面的某些复选框(.offset?)将被检查(如果它们还没有)

我不能使用单元格名称,因为上面的相同代码将适用于一堆列。

这就是我的那种

Set aaa = ActiveSheet.CheckBoxes(Application.Caller) 
With aaa.TopLeftCell 
    If aaa.Value = 1 Then 
        rsp = MsgBox("Check boxes below?", 4)
        If rsp = vbYes Then
             certain checkboxes(.offset?) below will be unchecked &
             .offset(0,0).value= "na"

1 个答案:

答案 0 :(得分:0)

假设您的复选框的位置如下图所示。我故意不对齐它们。

enter image description here

好的,这是我们将使用的一个小技巧。右键单击column 1中的所有复选框,然后单击Format Control。接下来转到Alt Text标签,然后输入1。对于第2列复选框,请在2中键入Alt text。对所有15列重复此操作。

enter image description here

现在将以下宏分配给所有顶部复选框。

Sub CheckBox_Click()
    Dim shp As Shape, MainShp As Shape
    Dim Alttext As String

    '~~> Get the checkbox which called the macro
    Set MainShp = ActiveSheet.Shapes(Application.Caller)
    '~~> Get it's alternate text
    Alttext = MainShp.AlternativeText

    '~~> Loop through all checkboxes except the checkbox which called it
    '~~> and check for their alt text
    For Each shp In ActiveSheet.Shapes
        If shp.Name <> MainShp.Name And shp.AlternativeText = Alttext Then
            '~~> If the top checkbox is checked then set the rest as checked
            If MainShp.OLEFormat.Object.Value = 1 Then
                shp.OLEFormat.Object.Value = 1
            Else
                shp.OLEFormat.Object.Value = 0
            End If
        End If
    Next
End Sub

现在,当您点击最顶部的复选框时,它会检查下面所有与顶部复选框具有相同Alt text的复选框。