Excel VBA 用户表单复选框访问

Excel VBA Userform CheckBoxes Access

我正在根据 sheet 范围内的行数以编程方式创建一个用户窗体(目前设置为用于测试的固定数字)。 然后用户选中他们想要选中的框并单击命令按钮。

用户窗体,如运行和下面的代码,手动添加了一个命令按钮和一个复选框。其他复选框以编程方式添加。 我不知道如何从有问题的复选框中获取值。我只是收到 "testbox" 未定义的错误。 我知道我错过了一些简单的东西...

有什么想法吗? 谢谢!

Option Explicit

Private Sub updateTablesBtn_Click()
    If CheckBox1.Value = True Then
        MsgBox "true"
    End If

    If testBox.Value = True Then
        MsgBox "true"
    End If
End Sub

Private Sub UserForm_Initialize()
    Dim chkBox As MSForms.CheckBox

    With formTableUpdate
        .Width = 150
        .Height = 200 '15 + 20 * (noOfVariants + 1) + 30
    End With

    Set chkBox = formTableUpdate.Controls.Add("Forms.CheckBox.1")
    With chkBox
        .Name = "testBox"
        .Caption = "test"
        .Left = 5
        .Top = 10
    End With

    With updateTablesBtn
        .Caption = "Update Tables"
        .Height = 25
        .Width = 76
        .Left = 38
        .Top = 30
    End With

End Sub

试试这个:

Dim chkBox As Control

For Each chkBox In formTableUpdate.Controls
    If chkBox.Name = "testBox" Then
        MsgBox chkBox.Caption & " has the value " & chkBox.Value
    End If
Next chkBox