ListBox in Word/VBA

K

Kevin Tang

Hello,

I have a ListBox in my VBA Form, named ListBox1
In UserForm_Initialize(), I have two statements to enable List Column Head:
ListBox1.ColumnCount = 2
ListBox1.ColumnHeads = True

My questions are:
1) how to set the Column Head's name? such as "ID", "Name"....
2) how to add item into ListBox1 in both columns?? (e.g. Add (00001, Kevin) )

Thanks,
Kevin Tang.
 
J

Jonathan West

Kevin Tang said:
Hello,

I have a ListBox in my VBA Form, named ListBox1
In UserForm_Initialize(), I have two statements to enable List Column Head:
ListBox1.ColumnCount = 2
ListBox1.ColumnHeads = True

My questions are:
1) how to set the Column Head's name? such as "ID", "Name"....

You can't. There is a bug in VBA and Microsoft never got round to fixing it.
Instead, set ColumnHeads to False, and position a couple of labels
immediately above the listbox whose captions will show the column headings.
2) how to add item into ListBox1 in both columns?? (e.g. Add (00001,
Kevin) )

Two possible approaches to this.

1. If you want to load the entire listbox in one go, put all your items into
a 2-dimensional array, and then assign the array to the List property of the
Listbox.

2. If you just want to add a single new row, use AddItem to add the new row
and assign text to the cell pointed to by the BoundCOlumn property (column 0
by default), Then add the text to the second column by using
ListBox1.List(n, 1) = "My new text", where n is the row number you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top