Listbox problem

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,

I am trying to copy a multi select (2 COLUMNS) selected item from 1 list box to another but im getting stuck. I can only seem to get the fisrt item

Ps.

List count startfrom 0
List index starts from 1
What just list start from i.e
listbox1.list(1) if i dont specify both arguments would that only pull through the 2nd item (1 column) as 1 in listindex referes to 1 where listcount referes to 0

Code:

Code:
Private Sub cbofwd_Click()
    Dim iCtr As Long
    For iCtr = 0 To Me.lst1.ListCount - 1
        If Me.lst1.Selected(iCtr) = True Then
            Me.lst2.AddItem Me.lst1.List [B]????[/B]
        End If
    Next iCtr
    For iCtr = Me.lst2.ListCount - 1 To 0 Step -1
        If Me.lst2.Selected(iCtr) = True Then
            Me.lst2.RemoveItem iCtr
        End If
    Next iCtr
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try...

Code:
Me.lst2.AddItem Me.lst1.List(iCtr, 0)

Hope this helps!
 
Upvote 0
Hi Domenic,

am i right in saying if i am moving a selected item from a listbox that has 2 or 3 columns, then i would need to have the 2nd argument of the list property set to 0

so me.listbox1.list(varselecteditem, 0)?

if i just want first item or if i ommit the 2nd arguement of the list or if i just have 1 column in the listbox then i can leave out the 2nd argument of the list? I.e (me.listbox1.list(varselecteditem)?

is that right?
 
Upvote 0
You need to use the 2nd argument of List to specify which column you are referring to.

For example Listbox1.List(1, 0) refers to the first column of the 2nd item in the listbox and Listbox1.List(3, 1) refers to the second column of the 4th item of the listbox.

if you omit the 2nd value you will get the value from the bound column which is usually, but not always, the first column.
 
Upvote 0
You need to use the 2nd argument of List to specify which column you are referring to.

For example Listbox1.List(1, 0) refers to the first column of the 2nd item in the listbox and Listbox1.List(3, 1) refers to the second column of the 4th item of the listbox.

if you omit the 2nd value you will get the value from the bound column which is usually, but not always, the first column.

Hi Norie,

so say I selected the 1st item in the listbox which has 3 columns, how do I copy the selected to listbox 2 with all 3 columns?

ps what bound column?

thanks
 
Upvote 0
Do both list boxes have the same number of columns?
If so, you could use something like this to move all the selected data from ListBox1 to ListBox2. (Assumes 3 columns each)

Code:
For I = 0 to ListBox1.ListCount - 1
    If ListBox1.Selected(I) then
        With ListBox2
            .AddItem ListBox1.List(I, 0)
            .List(.ListCount - 1, 1) = ListBox1.List(I, 1)
            .List(.ListCount - 1, 2) = ListBox1.List(I, 2)
        End With
    End If
Next i

If your second listbox has fewer columns than the first, which column of data do you want not moved?

Or do you want a concatenation moved, e.g.

Col1 Col2 Col3
becomes
Col1 Col2,Col3
 
Upvote 0
Something like this.
Code:
With ListBox1
    ListBox2.AddItem
    For I = 0 To .ColumnCount-1
        ListBox2.List(ListBox2.ListCount-1, i) = .List(.ListIndex, I)
    Next I
End With

BoundColumn is a property that sets which the column the value of the listbox will come from.
 
Upvote 0
To get the correct index in ListBox2 for the added items.

If you are specifically asking why -1 it's because the index is 0-based.
 
Upvote 0

Forum statistics

Threads
1,222,118
Messages
6,164,072
Members
451,870
Latest member
Nikhil excel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top