VBA从userform获取checkbox值

我无法从用户窗体的checkbox中获取值。 我遇到的问题是,用户窗体基于表单中的值创build可变数量的checkbox。 代码为:

Private Sub UserForm_Initialize() Dim LastRow As Long Dim i As Long Dim Teller As Long Dim chkBox As MSForms.CheckBox Teller = 1 LastRow = Worksheets("Sheet").Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To LastRow If Worksheets("Sheet").Cells(i, 1).Value = Worksheets("Sheet").Range("S1").Value Then Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & Teller) chkBox.Caption = Worksheets("Sheet").Cells(i, 9).Value chkBox.Left = 5 chkBox.Top = 25 + ((Teller - 1) * 20) Teller = Teller + 1 End If Next i End Sub 

所以这会创build一些名为CheckBox_1,CheckBox_2等checkbox。问题是,当我尝试获取CheckBox_1的值在模块中,CheckBox_1尚未创build,所以我无法使用它。

 Dim x as String With UserForm4 .Show x = .CheckBox_1 MsgBox (x) End End With 

您需要循环.Controls文本框不是您的窗体上的属性。