【发布时间】:2017-07-21 17:14:51
【问题描述】:
我无法从用户窗体的复选框中获取值。我遇到的问题是用户窗体根据工作表中的值创建可变数量的复选框。代码:
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
所以这会创建许多名为 CheckBox_1、CheckBox_2 等的复选框。 问题是当我尝试在模块中获取 CheckBox_1 的值时,尚未创建 CheckBox_1,所以我无法使用它。
Dim x as String
With UserForm4
.Show
x = .CheckBox_1
MsgBox (x)
End
End With
【问题讨论】: