How can I add items in a list with 2 columns? It adds the items just in the first column if I use ListBox.AddItem. I want to add items in the 2nd column too.
Thanks!
2 Answers
By using the List property.
ListBox1.AddItem "foo"
ListBox1.List(ListBox1.ListCount - 1, 1) = "bar"
6 Comments
Andrei Ion
Thanks a lot. Now if I want to get that value how can I do it? I mean to get the value in the first column I do ListBox1.Value ... bot for the 2nd column?
GSerg
@Andrei Ion In the same way.
List is a property, works both ways.Andrei Ion
Yes, I guessed that it has to be the same way but I won't know the row of the selected item... I know that the column is 1 but the row? I guess that ListBox1.ListCount-1 is the row when you add the value... how do I know what's the row when a Value is selected? Thanks a lot!
GSerg
@Andrei Ion The selected index is
.ListIndex. Please use the object browser instead of wanderng in the dark. Press F2. Find the property and press F1.Andrei Ion
Thanks. I try to use the help and it helps me a lot but sometimes I really don't know what to search and then I use stackoverflow. Anyway... thanks a lot... it really helped me!
|
There is one more way to achieve it:-
Private Sub UserForm_Initialize()
Dim list As Object
Set list = UserForm1.Controls.Add("Forms.ListBox.1", "hello", True)
With list
.Top = 30
.Left = 30
.Width = 200
.Height = 340
.ColumnHeads = True
.ColumnCount = 2
.ColumnWidths = "100;100"
.MultiSelect = fmMultiSelectExtended
.RowSource = "Sheet1!C4:D25"
End With End Sub
Here, I am using the range C4:D25 as source of data for the columns. It will result in both the columns populated with values.
The properties are self explanatory. You can explore other options by drawing ListBox in UserForm and using "Properties Window (F4)" to play with the option values.
2 Comments
FCastro
Not exactly the answer, but a more efficient method to add a multicolumn list.
sir KitKat
This only works when using VBA in combination with Excel :)