PDA

View Full Version : UserForm-ListBox



yn19832
03-21-2007, 09:16 AM
Thanks in advance for your help.
I want to add items to a listbox (named "ListCT"), each item include two element: one come from a ComboBox (named "ComType"), the other come from a listbox (named "ListCountry"), codes are as following for the "Add" button:

ReDim selArray(1 To 2, 0 To 5)
For i = 0 To 5
selArray(1, i) = LiqForm.ComType.Value
selArray(2, i) = LiqForm.ListCountry.Value
Next i

With ListCT

'Add Item to ListCT
.AddItem
i = .ListCount - 1
.List(i, 1) = selArray(1, i)
.List(i, 2) = selArray(2, i)

End With

But it only adds the value from the "ComType", can anyone give me some advice? many thanks.

JimmyTheHand
03-21-2007, 11:01 PM
Hi :hi:

What is the ColumnCount property of ListCT set to?

Jimmy

yn19832
03-22-2007, 02:04 AM
I have set the ColumnCount to 2, it seems not this problem.

many thanks

Charlize
03-22-2007, 02:46 AM
ReDim selArray(1 To 2, 0 To 5)
For i = 0 To 5
selArray(1, i) = LiqForm.ComType.Value
'selArray(i,0) ...
selArray(2, i) = LiqForm.ListCountry.Value
'selArray(i,1) ...
Next i
Why do you say Redim selArray(1 to 2, 0 to 5) ?
Maybe Redim Preserve selArray(6,2) Means 2 columns with 6 rows in it. 0 = 1 element , 1 = second until 5 = 6th element.
Referring to an array item begins with 0 but declaring it begins with 1.
And after filling your array, you sayListCT.List = selArray
Charlize

moa
03-22-2007, 02:57 AM
I may be wrong but... the first column in your listbox is column 0. When you use .AddItem it is adding nothing to the first column (0) then you are adding the combobox data to the second column (1) and the list box value to a non-displayed third column (2)

You should use something like...

.AddItem (selArray(1, i))
.List(i, 1) = selArray(2, i)


untested.

Also you could make it less confusing by making the array mirror the listbox with two columns and six rows rather than the other way around.

yn19832
03-22-2007, 04:18 AM
Why do you say Redim selArray(1 to 2, 0 to 5) ?
Maybe Redim Preserve selArray(6,2) Means 2 columns with 6 rows in it. 0 = 1 element , 1 = second until 5 = 6th element.
Referring to an array item begins with 0 but declaring it begins with 1.
And after filling your array, you sayListCT.List = selArray
Charlize


Many thanks.
I am confused. Does selArray(6, 2) means 7 columns and 3 rows, I am afraid I do not get your idea.

I tried this

ReDim Preserve selArray(2, 6)
For i = 1 To 6
selArray(1, i) = LiqForm.ComType.Value
selArray(2, i) = LiqForm.ListCountry.Value
Next i

It that correct ? It seems it does not work.

moa
03-22-2007, 04:29 AM
I didn't read your post correctly first time.

Using an array here seems pointless.

With ListCT
i = .ListCount - 1
.AddItem (LiqForm.ComType.Value)
.List(i, 1) = LiqForm.ListCountry.Value
End With

Charlize
03-22-2007, 04:43 AM
Many thanks.
I am confused. Does selArray(6, 2) means 7 columns and 3 rows, I am afraid I do not get your idea.

I tried this

ReDim Preserve selArray(2, 6)
For i = 1 To 6
selArray(1, i) = LiqForm.ComType.Value
selArray(2, i) = LiqForm.ListCountry.Value
Next i

It that correct ? It seems it does not work.
Declaring it : selArray(6, 2) means 6 rows and 2 columns. When you want to use the items of the array you start with 0 as the first item. selArray(5,0) means 6th item in first column.

Charlize

moa
03-22-2007, 05:02 AM
pointless.

...And the array looks like it is filling up wiht the same two values 6 times.

Charlize
03-22-2007, 05:13 AM
'Create array with 6 rows and 2 columns
ReDim Preserve selArray(6, 2)
'rows count from 0 to 5
'row 0 is first row in array
For i = 0 To 5
'fill row i, column 1 in array
selArray(i, 0) = LiqForm.ComType(i)
'fill row i, column 2 in array
selArray(i, 1) = LiqForm.ListCountry(i)
Next i
ListCT.list = selArray

moa
03-22-2007, 06:23 AM
:wot


I think yn19832 needs to clarify what he/she/it wants to do.

I am assuming that yn19832 is wanting, when the Add button is clicked, to add two values (selected in a combobox and listbox respectively) to one row in a list box.:dunno