Copy from one box to another on a user form

J

JLR-Mart

Please can someone help. I have a user form with two list boxes on it. One on
the left and one on the right. The LH box has a list of choices and the RH
box is empty. I want a user to highlight a choice or choices in the left box
and by clicking a button with an arrow on it pointing to the RH box it will
populate the RH box with this choice. Having created a list of choices in the
RH box I then want to create a space seperated string of these choices.

Anyone done something similar before?
 
J

John Bundy

Make sure you set your multiselect option to select multiple or extended,
then put this in the buttons click event.

For i = 0 To ListBox1.ListCount - 1
On Error Resume Next
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
End If
Next i
 
J

JLR-Mart

Thanks John, nearly there. If I wanted the selection to 'move' from one box
to the other rather than 'copy' how would that work? So if I've selected it
and put it in the box on the RH side it's no longer an option in the LH side?
 
J

John Bundy

just add the line i have commented
Private Sub CommandButton1_Click()

For i = 0 To ListBox1.ListCount - 1
On Error Resume Next
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
ListBox1.RemoveItem (i) 'add this
End If
Next i

End Sub
 
J

JLR-Mart

Thanks for the responses...much appreciated

John Bundy said:
just add the line i have commented
Private Sub CommandButton1_Click()

For i = 0 To ListBox1.ListCount - 1
On Error Resume Next
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
ListBox1.RemoveItem (i) 'add this
End If
Next i

End Sub
 

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

Top