检查复选框的值并相应地设置所有其他复选框的值

时间:2017-03-25 12:56:42

标签: excel vba excel-vba checkbox

我正在尝试创建类似“Mastercheckbox”的内容,它会自动检查我工作表上的所有其他复选框。所以当我点击“Mastercheckbox”时我正在尝试启动以下VBA代码我使用表单工具而不是Active X来创建我的复选框以确保我的VBA与任何机器兼容(我读过Active X可能会导致一些问题)并获得以下代码:

Sub Mastercheckbox()

Dim Chk As CheckBox

With ActiveSheet.Shapes("Mastercheckbox")
    If .ControlFormat.Value = False Or .ControlFormat.Value = True Then
        If .ControlFormat.Value = False Then
            For Each chk In ActiveSheet.CheckBoxes
                If Not chk.Name = "Mastercheckbox" Then
                chk.Value = False
                End If
            Next chk
        End If

        If Not .ControlFormat.Value = True Then
            For Each chk In ActiveSheet.CheckBoxes
                If Not chk.Name = "Mastercheckbox" Then
                chk.Value = True
                End If
            Next chk
        End If
     Else: MsgBox ("couldn't check value of mastercheckbox")
     End If
End With
End Sub

For循环中的所有内容都有效,但检查Mastercheckbox的值是因为某些原因而无法正常工作,而是直接跳到Else案例中。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

不是guess控件,您必须根据program p1; const MAX_GUESSES = 6; mysteryWord = 'abomination'; //comparetext(str1,str2); type word = array[0 .. 11] of char; var guesses : word; counter : integer;//for multipurpose counting ;) wrong : integer; keepGoing : boolean; guess : char; exists : boolean; begin guesses := '***********'; wrong := 0; keepGoing := true; repeat keepGoing := false; exists := false; writeln('your word is ' + guesses + '.'); write('You have '); //this one line is separated into 3 write(6-wrong); //because it wont allow (6-wrong) to writeln(' wrong guesses left!');//be put in line with strings. write('Guess a letter: '); read(guess); for counter := 0 to 11 do begin if (guess = mysteryWord[counter]) then begin guesses[counter-1] := guess; exists := true; end; end; if(exists = false) then wrong := wrong + 1; for counter := 0 to 11 do begin if(guesses[counter] = '*') then keepGoing := true; end; until ((wrong >= 6) or (keepGoing = false)); end. (对应于1)和ActiveX`(对应于-4146)检查其值

你也可以按如下方式重构你的代码:

xlOn

答案 1 :(得分:1)

由于您已决定使用User_Form复选框,因此您需要将变量定义为Shape。为了检查它们的价值,您需要检查它们是xlOn还是xlOff

为了看到您按下“Mastercheckbox”后读取的值,您可以使用下面的代码:

Dim MasterCB As Shape

Set MasterCB = ActiveSheet.Shapes("Mastercheckbox") '<-- set the MasterCB  variable

If MasterCB.ControlFormat.Value = xlOn Then
    MsgBox "Is checked"
ElseIf MasterCB.ControlFormat.Value = xlOff Then
    MsgBox "Not checked"
End If

您的帖子代码

Option Explicit

Sub Mastercheckbox()

Dim Chk As Shape
Dim MasterCB As Shape

' set the MasterCB  variable
Set MasterCB = ActiveSheet.Shapes("Mastercheckbox")

' loop through all shapes in ActiveSheet
For Each Chk In ActiveSheet.Shapes
    If Chk.Type = msoFormControl Then '<-- check your shape type is User_Form type
        If Chk.FormControlType = xlCheckBox Then '<-- check if the shape is a checkbox
            If Chk.Name <> "Mastercheckbox" Then 
                Chk.ControlFormat.Value = MasterCB.ControlFormat.Value '<-- modify all checkboxes to the value of "Mastercheckbox"
            End If
        End If
    End If
Next Chk

End Sub