Listbox1 to Listbox 2 Remove Item Once Selected

baller1013

New Member
Joined
Sep 7, 2017
Messages
15
I have a user form that has two listboxes (listbox1 and listbox2). When a user selects the value from listbox1 and adds it to listbox2, using command button, I would like the selection to disappear from listbox1. Additionally, if the user removes the selection from listbox2, I would like to add it back to listbox1. The listboxes are multicolumn (2) and multiselect. This is for a project so any help you can provide would be greatly appreciated. The following code I have that allows the user to select a value from listbox1 and add it to listbox2 is below. I have not been able to get the value to be removed from listbox1 as of yet. Thanks again!

Code:
If ListBox1.ListIndex = -1 Then Exit Sub

For i = 0 To ListBox1.ListCount - 1
     If ListBox1.Selected(i) = True Then
        ListBox2.AddItem ListBox1.List(i, 0)
        ListBox2.List(ListBox2.ListCount - 1, 1) = ListBox1.List(i, 1)
        ListBox1.RemoveItem

     End If
Next I

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
Code:
Private Sub CommandButton1_Click()
    
    Dim i As Long
    
    If ListBox1.ListIndex = -1 Then Exit Sub
    
    For i = ListBox1.ListCount - 1 To 0 Step -1
         If ListBox1.Selected(i) = True Then
            ListBox2.AddItem ListBox1.list(i, 0)
            ListBox2.list(ListBox2.ListCount - 1, 1) = ListBox1.list(i, 1)
            ListBox1.RemoveItem i
         End If
    Next i

End Sub
 
Upvote 0
Fluff- You are awesome! That worked perfectly!!!

I am going to try to do something very similar when a user chooses to remove the selection from listbox2; I want to add the value back to listbox1. Thanks again!!
 
Upvote 0
Fluff- You are awesome! That worked perfectly!!!
Glad to help & thanks for the feedback.


I am going to try to do something very similar when a user chooses to remove the selection from listbox2; I want to add the value back to listbox1.
Good luck. If you have any problems, post back
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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