Alec
Alec

Reputation: 141

VBA concatenate TextBox and ListBox

I'm new to VBA and I'm wondering what is the best way to Concatenate the Value of the second Column in a ListBox with a TextBox that is filled out by the user?

My code so far:

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

Now I have TextBox aka DWG where the user is typing in about 5 numbers or so.

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

Here is my userForm that I'm testing: enter image description here

Upvotes: 1

Views: 1465

Answers (1)

user6432984
user6432984

Reputation:

You could set either the ListBox1.Bound or Listbox1.TextColumn to 1 or you can use the ListBox1.ListIndex to lookup the value in the Listbox's List.

Private Sub ListBox1_Click()

    Me.Caption = ListBox1.Value
    TextBox1.Value = TextBox1.Value & ListBox1.List(ListBox1.ListIndex, 1)

End Sub

enter image description here

Upvotes: 1

Related Questions