Re: Populate a Multi-Column ListBox



Doug,

Yep I agree that it may be a daft idea, but as I said it is a practical
exericise and the VB Help clearly indicates to me at least the I should
be able to write or read a "column of values" using the .Column
property.

Here is an example of what I am trying to demonstrate. I want to
populate a 2 column listbox with no external source using just the
initialize event. For example, say I wanted to list the U.S. state
names and then their abbreviations "hidden" in column 2. When the user
selects a state name, the userform inserts the abbreviation for that
state in the document.

It seems to me that I should somehow be able to directly populate
ListBox column 1 with an array of state names created using the Split
method (I can) and then populate Listbox column 2 with an array of
abbreviations created using the Split method (I can't).

I can do that by first creating an array with the split funtion of the
state names. Then create another array using the split function of the
abbreviations, then a third two dimensional array using the first two.
I can then populate the listbox using the third array.

Private Sub UserForm_Initialize()
Dim myArray1 As Variant
Dim myArray2 As Variant
Dim myArray3() As String
Dim i As Long
Dim oCount As Long
myArray1 = Split("Alabama|Alaska|Arizona|Etc.", "|")
myArray2 = Split("AL|AK|AZ|Etc", "|")
oCount = UBound(myArray1)
ReDim myArray3(oCount, 1)
For i = 0 To UBound(myArray1)
myArray3(i, 0) = myArray1(i)
Next i
For i = 0 To UBound(myArray2)
myArray3(i, 1) = myArray2(i)
Next i
ListBox1.ColumnCount = 2
ListBox1.ColumnWidths = "60;0"
ListBox1.List = myArray3
End Sub


Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Me.ListBox1.TextColumn = 2
Set oRng = ActiveDocument.Bookmarks("State").Range
oRng.Text = Me.ListBox1.Text
ActiveDocument.Bookmarks.Add "State", oRng
Me.Hide
End Sub





Doug Robbins - Word MVP wrote:
Sorry Greg,

I think that is a daft idea. I do not consider a list box to be appropriate
place to be linking items together. They should be linked at their source.

If on the other hand you wanted the list box to display non-contiguous
columns from the one data source, that can be accomplished by setting the
width of the intervening columns to zero.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:ORA1geqOHHA.780@xxxxxxxxxxxxxxxxxxxxxxx
I know how to populate a multi-column listbox one item at a time using
AddItem and List. I know how to populate a single column ListBox using List
and an array.

I am trying to figure out code that will populate column 1 of of a two
colunm listobox with the contents of one array and column 2 with the
contents of a second different array (I realize a better way might be to
populate the listbox with a multi-dimensional array but as a practical
exercise I would like to work it out the way I described)

Here is the code I have put together.
Private Sub UserForm_Initialize()
Dim myArray As Variant
With ListBox1
.ColumnCount = 2
.AddItem "1"
.List(0, 1) = "A"
.AddItem "2"
.List(1, 1) = "B"
.AddItem "3"
.List(2, 1) = "C"
.AddItem "Etc."
.ColumnWidths = "90;90"
End With
With ListBox2
myArray = Split("1|2|3|4|5", "|")
.List = myArray
End With
'Code works to this point. Now I would like to make ListBox2 a 2 column
ListBox. Populate column 1 with the first myArray then populate column 2
with the second.
'When I run the code below, nothing is placed in the second column. Any
ideas?

With ListBox2
.ColumnCount = 2
.BoundColumn = 1
myArray = Split("1|2|3|4|5", "|")
.List = myArray
.BoundColumn = 2
myArray = Split("A|B|C|D|E", "|")
.List = myArray
End With
End Sub



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

.


Loading