在Excel VBA中引用自身的复选框

时间:2015-07-23 17:48:52

标签: excel-vba vba excel

我有一个Excel电子表格,它将单元格的值设置为组中复选框的数量。我想为每个看起来像这样的宏分配一个宏:

Sub clickedBox()
    If thisBox(or however you would do it).Checked = True Then
        Range("D9").Value = Range("D9").Value + 1
    Else
        Range("D9").Value = Range("D9").Value - 1
    End If
End Sub

单元格默认为0,所有框默认为未选中。这样一来,勾选一个方框就可以计算出来,然后将其扣掉,将其击倒,并且它永远不会低于零或高于方框的数量。

我意识到我也应该这样做,以便当复选框的状态发生变化时宏不会触发,不仅仅是在点击它时,我还要确保这是可行的。

有没有办法让复选框像这样引用?

1 个答案:

答案 0 :(得分:5)

这取决于您是否与ActiveX控件或表单控件绑定。两者都可以工作,任何一条路径都可能指示如何清楚地实现它。

使用ActiveX控件(复选框):

您可以使用两种方法为ActiveX控件编写“点击处理程序”。第一个是为每个控件硬编码一个公共子:

控制Thisworkbook.Sheets("Sheet1")CheckBox1

Excel对象Sheet1中的

代码:

Private groupCheckBoxCount As Integer

Private Sub CheckBox1_Click()
    Debug.Print "Control on " & Me.Name & " is now " & Me.CheckBox1.Value
    RegisterCheckedValue Me.CheckBox1.Value
End Sub

Private Sub RegisterCheckedValue(cbVal As Boolean)
    If cbVal = True Then
        Range("CheckBoxCount") = Range("CheckBoxCount") + 1  'choose to store on the sheet
        groupCheckBoxCount = groupCheckBoxCount + 1          'or in a variable
    Else
        Range("CheckBoxCount") = Range("CheckBoxCount") - 1
        groupCheckBoxCount = groupCheckBoxCount - 1
    End If
End Sub

然后,如果你有10个复选框,你将有10个CheckBox(x)_Click个子,每个子项专门绑定到一个ActiveX控件。这些点击处理程序中的每一个都可以增加或减少存储在工作表单元格中(或模块私有变量中)的计数器。

第二个选项是创建一个类模块,您可以为任意数量的CheckBox实例化。

类模块MyCheckBoxClass的代码

Dim WithEvents cbControl As MSForms.CheckBox

Private controlName As String

Public Sub cbControl_Click()
    Debug.Print controlName & " is now " & cbControl.Value
    If cbControl.Value = True Then
        Range("CheckBoxCount") = Range("CheckBoxCount") + 1  'choose to store on the sheet
        groupCheckBoxCount = groupCheckBoxCount + 1          'or in a variable
    Else
        Range("CheckBoxCount") = Range("CheckBoxCount") - 1
        groupCheckBoxCount = groupCheckBoxCount - 1
    End If
End Sub

Public Sub Attach(newCB As MSForms.CheckBox, newName As String)
    Set cbControl = newCB
    controlName = newName
End Sub

Private Sub Class_Initialize()
    controlName = ""
End Sub

常规代码模块中的代码:

Public groupClickCount As Integer
Private cbCollection As Collection

Public Sub SetUpControlsOnce()
    Dim thisCB As MyCheckBoxClass
    Dim ctl As OLEObject
    Dim cbControl As MSForms.CheckBox

    If cbCollection Is Nothing Then
        Set cbCollection = New Collection
    End If

    For Each ctl In ThisWorkbook.Sheets("Sheet1").OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            '--- this is an ActiveX CheckBox
            Set thisCB = New MyCheckBoxClass
            thisCB.Attach ctl.Object, ctl.name
            cbCollection.Add thisCB
        End If
    Next ctl
End Sub

使用表单控件(复选框):

虽然有多种方法可以捕获表单复选框的click事件,但最简单的方法是将组中的所有复选框连接到单个宏:

Public groupClickCount As Integer

Public Sub cbControl_Click()
    '--- loop through all the controls on the form and filter for
    '    only checkboxes, then count up how many are checked
    Dim ctl As Shape
    Dim checkCount As Integer
    checkCount = 0
    For Each ctl In ActiveSheet.Shapes
        If ctl.Type = msoFormControl Then
            On Error Resume Next
            If ctl.ControlFormat = xlCheckBox Then
                If ctl.ControlFormat.Value = 1 Then
                    checkCount = checkCount + 1
                Else
                    checkCount = checkCount - 1
                End If
            End If
        End If
    Next ctl
    Range("CheckBoxCount") = checkCount 'choose to store on the sheet
    groupClickCount = checkCount        'or in a variable
End Sub

根据您的需求以及您希望如何跟踪复选框,这两种解决方案都可以通过多种方式进行调整。