继续运行时错误424

时间:2018-02-22 11:38:30

标签: excel vba excel-vba

我有这个代码,它根据文本框号的用户输入动态创建文本框和标签。但我得到了

  

424错误

我尝试使用 F8 进行调试。

我将有一个列(动态更新),使用该列来创建标签,列项的数量是文本框的数量(将用列的数量替换输入框。)

Dim number As Long

Private Sub UserForm_Initialize()
Dim i As Long
number = InputBox("enter the number")
Dim txtB1 As Control
For i = 1 To number
    Set txtB1 = Controls.Add(“Forms.TextBox1”)
    With txtB1
    .Name = “txtBox” & i
    .Height = 20
    .Width = 50
    .Left = 70
    .Top = 20 * i * 1
    End With
Next i

Dim lblL1 As Control
For i = 1 To number
    Set lblL1 = Controls.Add(“Forms.Label1”)
    With lblL1
    .Caption = “Label” & i
    .Name = “lbl” & i
    .Height = 20
    .Width = 50
    .Left = 20
    .Top = 20 * i * 1
    End With
Next i

Dim q As Long
For q = 1 To number
    Controls(“lbl” & q) = Cells(1, q)
Next q

End Sub

Private Sub CommandButton1_Click()
Dim p As Long
Dim erow As Long
erow = "Sheet3!A2:A" & Range("A" & Rows.Count).End(xlUp).Row

For p = 1 To number

    Cells(erow, p) = Controls(“txtBox” & p).Text

Next p

End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub
  

424错误显示此行存在问题

Set txtB1 = Controls.Add(“Forms.TextBox1”)

提前致谢

1 个答案:

答案 0 :(得分:3)

如上所述,动态创建文本框的正确字符串是:

  

Forms.TextBox.1

注意额外的句号.。请参阅here以供参考。

Set txtB1 = Controls.Add("Forms.TextBox.1")

结束评论中提出的其他观点:

  1. 您可以添加一个明确的Me,以便更清楚地了解控件的位置,即Me.Controls(...)。但排除它将始终隐式链接到正确的用户形式。
  2. 请注意使用"而不是