您好我已经复制/粘贴了TheEngineer-answer中的代码 - 我稍微修改了代码,因此它从数组而不是工作表中收集数据。我一直收到RuneTime Error 424
,当我在Error 424
上阅读MS帮助时,它说我应该启用Microsoft DAO 3.5 Object Library
我的Excel只有3.6
。我想新版本?但我仍然收到错误。有人能帮助我吗?
这是代码:
Option Explicit
Private Sub UserForm_Initialize()
Dim LastColumn As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
Call test ' Here i run array code (The array is filled with data)
TestArr = UniqueProvisionArray
LastColumn = UBound(TestArr)
For i = 0 To LastColumn
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = TestArr(i).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub
答案 0 :(得分:3)
由于行chkBox.Caption = TestArr(i).Value
,您收到该错误。这是从数组中检索数据的错误方法。
以下是使其有效的示例代码。
Private Sub UserForm_Initialize()
Dim LastColumn As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
Dim TestArr(1)
TestArr(0) = 1
TestArr(1) = 2
LastColumn = UBound(TestArr)
For i = 0 To LastColumn
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = TestArr(i)
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub
另一件事......
您可能希望将chkBox.Top = 5 + ((i - 1) * 20)
更改为chkBox.Top = 5 + (i * 20)
,否则您的第一个复选框将不可见;)