gpt4 book ai didi

arrays - 动态添加复选框 - 运行时错误 424

转载 作者:行者123 更新时间:2023-12-02 18:17:16 25 4
gpt4 key购买 nike

您好,我已经复制/粘贴了 TheEngineer-answer 中的代码 - 我稍微修改了代码,因此它从数组而不是工作表中收集数据。我不断收到 RuneTime 错误 424,当我阅读有关 错误 424 的 MS 帮助时,它说我应该仅在 Excel 中启用 Microsoft DAO 3.5 对象库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) 否则您的第一个复选框将不可见;)

关于arrays - 动态添加复选框 - 运行时错误 424,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com