嗨,我已经从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
最佳答案
您由于行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)
,否则您的第一个复选框将不可见;)