vba listbox multicolumn add [duplicate]
Possible Duplicate:
Adding items in a Listbox with multiple columns
With MFC VC++ there are two controls, ListBox
and ListCtrl
. But with VBA it seems we have only ListBox
.
I want to create a listbox with 2 columns (Company_ID, Company_Name).
Here is what I tried:
- I created lstbox(control type ListBox)
- Row source type = value list
- I am taking value from user from two edit boxes and when user clicks "add" then it should be added to the listbox with 2 columns.
In the VBA code routine I added the following lines:
lstbox.ColumnCount = 2
lstbox.AddItem (Company_ID)
The following code is not working which seems to be related with adding column value:
lstbox.Column(1,lstbox.ListCount - 1) = Company_name
This gives error:
Runtime error '424' object required.
Could anyone help with vba code to add to multi column listbox.
Solution 1:
Simplified example (with counter):
With Me.lstbox
.ColumnCount = 2
.ColumnWidths = "60;60"
.AddItem
.List(i, 0) = Company_ID
.List(i, 1) = Company_name
i = i + 1
end with
Make sure to start the counter with 0, not 1 to fill up a listbox.