I have this code and it dynamically creates text boxes and labels based on the user input for text box number. But I am getting
我有这个代码,它根据文本框号的用户输入动态创建文本框和标签。但我得到了
424 error
I tried to debug using F8.
我尝试使用F8进行调试。
I will have a column(dynamically updated) using which the labels have to be created and the count of the column items are the number of textboxes (will replace the input box with count of the column.)
我将有一个列(动态更新),使用该列来创建标签,列项的计数是文本框的数量(将用列的数量替换输入框。)
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 error is showing problem with this line
424错误显示此行有问题
Set txtB1 = Controls.Add(“Forms.TextBox1”)
Thanks in advance
提前致谢
1 个解决方案
#1
3
As mentioned already, the correct string to create a textbox on the fly is this:
如前所述,动态创建文本框的正确字符串是:
Forms.TextBox.1
Notice the additional period .
. See here for reference.
请注意附加期间..请参阅此处以供参考。
Set txtB1 = Controls.Add("Forms.TextBox.1")
To wrap up the other points made in the comments too:
要结束评论中提出的其他观点:
- You can add an explicit
Me
to make it even more clear where the controls live, i.e.Me.Controls(...)
. But excluding it will always implicitly link to the correct userform. - Just be careful that you use
"
rather than“
您可以添加一个明确的Me,以使控件生效的位置更加清晰,即Me.Controls(...)。但排除它将始终隐式链接到正确的用户形式。
小心你用“而不是”
#1
3
As mentioned already, the correct string to create a textbox on the fly is this:
如前所述,动态创建文本框的正确字符串是:
Forms.TextBox.1
Notice the additional period .
. See here for reference.
请注意附加期间..请参阅此处以供参考。
Set txtB1 = Controls.Add("Forms.TextBox.1")
To wrap up the other points made in the comments too:
要结束评论中提出的其他观点:
- You can add an explicit
Me
to make it even more clear where the controls live, i.e.Me.Controls(...)
. But excluding it will always implicitly link to the correct userform. - Just be careful that you use
"
rather than“
您可以添加一个明确的Me,以使控件生效的位置更加清晰,即Me.Controls(...)。但排除它将始终隐式链接到正确的用户形式。
小心你用“而不是”