【问题标题】:VBA concatenate TextBox and ListBoxVBA 连接文本框和列表框
【发布时间】:2016-09-27 21:31:38
【问题描述】:

我是 VBA 新手,我想知道将 ListBox 中第二列的值与用户填写的 TextBox 连接起来的最佳方法是什么?

到目前为止我的代码:

Private Sub UserForm_Initialize()

With ListBox1
.ColumnCount = 2
.ColumnHeads = False
.ColumnWidths = "30;15"
.MultiSelect = 0

ListBox1.AddItem "AAA"
ListBox1.AddItem "BBB"
ListBox1.AddItem "CCC"
ListBox1.AddItem "DDD"
ListBox1.List(0, 1) = 15
ListBox1.List(1, 1) = 19
ListBox1.List(2, 1) = 49
ListBox1.List(3, 1) = 45

    End With
End Sub

现在我有 TextBox aka DWG,用户在其中输入大约 5 个数字左右。

Private Sub DWG_change
    Dwg= "Numbers" & ListBox."SecondColumnValue"
End Sub

这是我正在测试的用户表单:

【问题讨论】:

    标签: excel textbox listbox concatenation vba


    【解决方案1】:

    您可以将ListBox1.BoundListbox1.TextColumn 设置为1,也可以使用ListBox1.ListIndex 在列表框的列表中查找值。

    Private Sub ListBox1_Click()
    
        Me.Caption = ListBox1.Value
        TextBox1.Value = TextBox1.Value & ListBox1.List(ListBox1.ListIndex, 1)
    
    End Sub
    

    【讨论】:

    • 天啊,我花了两天时间找这个!太感谢了 !!!非常感谢社区!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多