Andrei Ion
Andrei Ion

Reputation: 1827

Adding items in a Listbox with multiple columns

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!

Upvotes: 11

Views: 133247

Answers (2)

Srikanth Sharma
Srikanth Sharma

Reputation: 2067

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.

Upvotes: 3

GSerg
GSerg

Reputation: 78210

By using the List property.

ListBox1.AddItem "foo"
ListBox1.List(ListBox1.ListCount - 1, 1) = "bar"

Upvotes: 25

Related Questions