使用vba在运行时将多个标签和文本框添加到Excel用户窗体 [英] adding multiple labels and textboxes to an Excel userform during runtime using vba

查看:425
本文介绍了使用vba在运行时将多个标签和文本框添加到Excel用户窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Excel VBA创建库存管理工具.我已经创建了从Internet Explorer的下拉框中收集名称列表并将其放入数组的代码.

I'm creating an inventory management tool with Excel VBA. I've created code that gathers a list of names from a drop down box on Internet Explorer and puts them into an array.

我需要做的类似于 vba创建多个文本框的操作组合框以用户形式动态显示,但是我将为用户名动态添加标签,并为每个人将收到的FLN数量动态添加文本框.然后,这些将进入我已经创建的预定义用户表单.

What I need to do is something similar to vba create several textboxes comboboxes dynamically in userform, but I would be dynamically adding labels for the user names and textboxes for the number of FLNs each person would be receiving. These would then go into a predefined userform I've already created.

按照上面的代码示例,我意识到我无法使用.Name = "Textbox" & i重命名下一个标签或文本框. i必须等于一个不断变化的列表,因此不能一成不变.因此,为什么标签和文本框必须与UBound(UserArray)一样多.

Per the code example above, I realize I can't use .Name = "Textbox" & i to rename the next label or textbox. i has to equal to an ever-changing list, so it can't be set in stone; hence why there has to be as many labels and textboxes as 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

推荐答案

该问题的答案具有误导性.该问题想在通过更改控件的ProgID添加控件时提供默认名称(bstrProgID是引用要创建的类的字符串).

Lou the answer to the question is misleading. The question wants to provide a default name when adding the control by changing its ProgID ( bstrProgID is a string that references the class that is to be created).

如果另一个控件没有相同的名称,则可以重命名新控件.

You can rename the new controls provided that another control does not have the same name.

您还可以将控件名称作为参数传递给Controls.Add方法.

You can also pass the controls name as an argument to the Controls.Add method.

您未显示的标签是您从未设置Label.Caption值.

Your labels are not showing is that you never set the Label.Caption value.

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

这篇关于使用vba在运行时将多个标签和文本框添加到Excel用户窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆