Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vba listbox multicolumn add [duplicate]

Tags:

vba

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:

  1. I created lstbox(control type ListBox)
  2. Row source type = value list
  3. 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.

like image 876
Jignesh Makwana Avatar asked Jun 26 '12 18:06

Jignesh Makwana


1 Answers

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.

like image 53
html_programmer Avatar answered Sep 24 '22 20:09

html_programmer