11

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 2

25

By using the List property.

ListBox1.AddItem "foo"
ListBox1.List(ListBox1.ListCount - 1, 1) = "bar"
Sign up to request clarification or add additional context in comments.

6 Comments

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?
@Andrei Ion In the same way. List is a property, works both ways.
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!
@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.
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!
|
3

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

Not exactly the answer, but a more efficient method to add a multicolumn list.
This only works when using VBA in combination with Excel :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.