简体   繁体   中英

Excel VBA Form Checkbox

Hey I am new to Excel VBA and need someone's help. Following is what I am looking for:

If a certain unlinked (form) checkbox(topleftcell?) is checked Then certain checkboxes below it(.offset?) will be checked off (if they are not already)

I can't use cell names since the same code above will apply to bunch of columns.

This is what I have sort of

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"

Let's say that your checkboxes are placed as shown in the image below. I deliberately didn't align them.

在此处输入图片说明

Ok here is a small trick that we will use. Right click on all checkboxes in column 1 one by one and click on Format Control . Next go to the Alt Text tab and type 1 . For Column 2 checkboxes, type 2 in the Alt text . Repeat this for all 15 columns.

在此处输入图片说明

Now assign the below macro to all the top checkboxes.

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

Now when you click the top most checkbox then it will check all the checkboxes below which have the same Alt text as the top checkbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM