使用 vba 在运行时向 Excel 用户窗体添加多个标签和文本框
adding multiple labels and textboxes to an Excel userform during runtime using vba
我正在使用 Excel VBA 创建库存管理工具。我创建了从 Internet Explorer 的下拉框中收集名称列表并将它们放入数组中的代码。
我需要做的是类似于 的事情,但我会为用户名动态添加标签,并为每个人将收到的 FLN 数量添加文本框。然后这些将进入我已经创建的预定义用户表单。
根据上面的代码示例,我意识到我不能使用 .Name = "Textbox" & i
来重命名下一个标签或文本框。 i
必须等于 ever-changing 列表,因此不能一成不变;因此为什么必须有与 UBound(UserArray)
.
一样多的标签和文本框
已更新
Private Sub CreateControl()
Dim newTxt As msforms.Control, newLbl
Dim i As Integer, TopAmt
Dim UserArray As String
TopAmt = 30
For i = LBound(MyArray) + 1 To UBound(MyArray) - 1
Set newLbl = MultipleOptionForm.Controls.Add("Forms.Label.1")
With newLbl
.Name = "Label" & i
.Left = 10
.Top = TopAmt
.WordWrap = False
.AutoSize = True
.Visible = True
.Caption = MyArray(i)
Debug.Print .Name,
End With
Set newTxt = MultipleOptionForm.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxt
.Left = 150
.Top = TopAmt
.Visible = True
.Width = 20
Debug.Print .Name
End With
TopAmt = TopAmt + newTxt.Height
Next
MultipleOptionForm.Show
End Sub
Lou 问题的答案具有误导性。该问题希望在通过更改其 ProgID 添加控件时提供默认名称( bstrProgID 是一个引用要创建的 class 的字符串)。
您可以重命名新控件,前提是另一个控件的名称不同。
您还可以将控件名称作为参数传递给 Controls.Add
方法。
您的标签未显示是因为您从未设置 Label.Caption
值。
Private Sub CreateControl()
Dim newLbl As MSForms.Label
Dim newTxt As MSForms.Control
Dim i As Integer, TopAmt
Dim UserArray As Variant
TopAmt = 50
UserArray = Array("Cat", "Dog", "Horse", "Gorrilla")
For i = LBound(UserArray) To UBound(UserArray)
Set newLbl = MultipleOptionForm.Controls.Add("Forms.Label.1")
With newLbl
.Name = "Label" & i
.Left = 50
.Top = TopAmt
.Visible = True
.Caption = UserArray(i)
Debug.Print .Name,
End With
Set newTxt = MultipleOptionForm.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxt
.Left = 100
.Top = TopAmt
.Visible = True
Debug.Print .Name
End With
TopAmt = TopAmt + newTxt.Height
Next
End Sub
下一期:如何从这些动态创建的文本框中获取数据?
Dim newTxt As MSForms.Control
For i = LBound(UserArray) To UBound(UserArray)
set newTxt = MultipleOptionForm.Controls("Textbox" & i)
If UserArray(i) <> newTxt.Value then
'Do something
End if
Next
我正在使用 Excel VBA 创建库存管理工具。我创建了从 Internet Explorer 的下拉框中收集名称列表并将它们放入数组中的代码。
我需要做的是类似于
根据上面的代码示例,我意识到我不能使用 .Name = "Textbox" & i
来重命名下一个标签或文本框。 i
必须等于 ever-changing 列表,因此不能一成不变;因此为什么必须有与 UBound(UserArray)
.
已更新
Private Sub CreateControl()
Dim newTxt As msforms.Control, newLbl
Dim i As Integer, TopAmt
Dim UserArray As String
TopAmt = 30
For i = LBound(MyArray) + 1 To UBound(MyArray) - 1
Set newLbl = MultipleOptionForm.Controls.Add("Forms.Label.1")
With newLbl
.Name = "Label" & i
.Left = 10
.Top = TopAmt
.WordWrap = False
.AutoSize = True
.Visible = True
.Caption = MyArray(i)
Debug.Print .Name,
End With
Set newTxt = MultipleOptionForm.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxt
.Left = 150
.Top = TopAmt
.Visible = True
.Width = 20
Debug.Print .Name
End With
TopAmt = TopAmt + newTxt.Height
Next
MultipleOptionForm.Show
End Sub
Lou 问题的答案具有误导性。该问题希望在通过更改其 ProgID 添加控件时提供默认名称( bstrProgID 是一个引用要创建的 class 的字符串)。
您可以重命名新控件,前提是另一个控件的名称不同。
您还可以将控件名称作为参数传递给 Controls.Add
方法。
您的标签未显示是因为您从未设置 Label.Caption
值。
Private Sub CreateControl()
Dim newLbl As MSForms.Label
Dim newTxt As MSForms.Control
Dim i As Integer, TopAmt
Dim UserArray As Variant
TopAmt = 50
UserArray = Array("Cat", "Dog", "Horse", "Gorrilla")
For i = LBound(UserArray) To UBound(UserArray)
Set newLbl = MultipleOptionForm.Controls.Add("Forms.Label.1")
With newLbl
.Name = "Label" & i
.Left = 50
.Top = TopAmt
.Visible = True
.Caption = UserArray(i)
Debug.Print .Name,
End With
Set newTxt = MultipleOptionForm.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxt
.Left = 100
.Top = TopAmt
.Visible = True
Debug.Print .Name
End With
TopAmt = TopAmt + newTxt.Height
Next
End Sub
下一期:如何从这些动态创建的文本框中获取数据?
Dim newTxt As MSForms.Control
For i = LBound(UserArray) To UBound(UserArray)
set newTxt = MultipleOptionForm.Controls("Textbox" & i)
If UserArray(i) <> newTxt.Value then
'Do something
End if
Next