Populate a listbox from another listbox

blk

Board Regular
Joined
May 27, 2003
Messages
61
Hi,

I have created a userform. It has two listboxes in the form and an add, remove and reset button in the middle of the two boxes.

The listbox on the left is populated from a range in the same workbook. I've changed the listbox properties to multiselect, so the user can select one or many items from the list.

What i'd like to do is be able to select an item/s from the list on the left, and then when the add button is pressed, move the item/s to the box on right.

I've added a remove button in case the user moves the wrong item, and a reset button if the user wants to start again.

I've tried for hours but just can't figure out how to do it.


Thanks


Ben
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
This would be the code for your add button...

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> ListBox1.ListCount - 1
        <SPAN style="color:#00007F">If</SPAN> ListBox1.Selected(i) <SPAN style="color:#00007F">Then</SPAN>
            ListBox2.AddItem ListBox1.List(i, 0)
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
Thanks for that. It's got me of to a good start, but is there any way to remove the origionally selected items from listbox1 once it has move accross to listbox2. This is so it can't be selected twice.

Thanks in advance for any help you can give me....
 
Upvote 0
Here's what I've tried, but it doesn't work.

Private Sub Add_Click()

Dim i As Long
For i = 0 To AllPOCS.ListCount - 1
If AllPOCS.Selected(i) Then
POCselect.AddItem AllPOCS.List(i, 0)
End If
Next

'''''TO REMOVE THE SELECTED ITEMS FROM ALLPOCS

For i = 0 To AllPOCS.ListCount - 1
If AllPOCS.Selected(i) Then
AllPOCS.RemoveItem AllPOCS.List(i, 0)
End If
Next

End Sub
 
Upvote 0
The problem with the RemoveItem command is that the syntax is wrong. However, even with the correct syntax it still will not work because your list is built upon a RowSource.

If your list was not built upon a RowSource, then this would work...

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">For</SPAN> i = (ListBox1.ListCount - 1) <SPAN style="color:#00007F">To</SPAN> 0 <SPAN style="color:#00007F">Step</SPAN> -1
        <SPAN style="color:#00007F">If</SPAN> ListBox1.Selected(i) <SPAN style="color:#00007F">Then</SPAN>
            ListBox2.AddItem ListBox1.List(i, 0), 0
            ListBox1.RemoveItem i
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0

Forum statistics

Threads
1,207,285
Messages
6,077,529
Members
446,288
Latest member
lihong3210

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