Excel VBA Userformcheckbox访问

我正在编程创build一个基于工作表范围内的行数(当前设置为一个固定的数字进行testing)的用户窗体。 然后用户检查他们想要检查的框,然后点击命令button。

用下面的代码运行的用户表单有一个命令button和一个手动添加的checkbox。 其他checkbox以编程方式添加。 我无法弄清楚如何从创build的checkbox有问题的价值。 我只是得到一个错误,“testing箱”没有定义。 我知道我错过了一些简单的…

有什么想法吗? 谢谢!

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