PDA

View Full Version : Moving Items Between ListBoxes



MWE
06-14-2006, 10:31 AM
I have two list boxes in a UserForm: lstbxA and lstbxB. Initially, lstbxA is filled with some items; lstbxB is blank. I wish to move an item, say "X" from lstbxA to lstbxB. That process is pretty obvious. But in doing so, I wish to "remove "X" from lstbxA so it is no longer a choice. That is not so obvious. I can loop through the items in lstbxA, find "X" and then delete that item. That seems cumbersome. Is there a way to directly delete "X" from lstbxA?

Norie
06-14-2006, 11:04 AM
Are you moving multiple items?

mdmackillop
06-14-2006, 11:13 AM
Try

Private Sub UserForm_Initialize()
For i = 1 To 10
ListBox1.AddItem "Data" & i
Next
End Sub
Private Sub CommandButton1_Click()
Dim tmp As Long
tmp = ListBox1.ListIndex
ListBox2.AddItem ListBox1
ListBox1.RemoveItem (tmp)
End Sub



or

http://support.microsoft.com/kb/213756/EN-US if list comes from your spreadsheet.

Regards
MD

MWE
06-14-2006, 02:07 PM
Try

Private Sub UserForm_Initialize()
For i = 1 To 10
ListBox1.AddItem "Data" & i
Next
End Sub
Private Sub CommandButton1_Click()
Dim tmp As Long
tmp = ListBox1.ListIndex
ListBox2.AddItem ListBox1
ListBox1.RemoveItem (tmp)
End Sub


http://support.microsoft.com/kb/213756/EN-US if list comes from your spreadsheet.

Regards
MDThanks. Seems so obvious when shown ...

So, after moving items back a forth, I want to sort (the items in) both ListBoxes. Again, I know how to do this via brute force, using, say, a bubble sort, but is there an elegant way? perhaps something built into the ListBox properties or methods

mdmackillop
06-14-2006, 03:00 PM
Can't think how to achieve this without an "external" sort. You could write/read the listbox items to a hidden sheet and use dynamic ranges and worksheet sorts, but I'm not sure that is any more elegant.