Listbox2.removeitem

rwheeler

Board Regular
Joined
Apr 22, 2010
Messages
105
I have this code to add items to listbox2 from listbox1. How can I code a Commandbutton4 to remove selected items in listbox2

Code:
Private Sub CommandButton3_Click()
Dim i As Integer
For i = ListBox1.ListCount - 1 To 0 Step -1
If ListBox1.Selected(i) Then
ListBox2.AddItem (ListBox1.List(i))
End If
Next
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Untested

Code:
With UserForm1.textbox2
 .AddItem ""
 .ListIndex = .ListCount - 1
 .RemoveItem .ListCount - 1
End With
 
Upvote 0
If you want to see if the item is Hello, then try

Code:
For i = ListBox1.ListCount - 1 To 0 Step -1
    If ListBox2.List(i) = "Hello" Then
        ListBox2.RemoveItem (i)
    End If
Next i
 
Last edited:
Upvote 0
Your work well. I did not have any luck with his, though I do appreciate the reply. I think all i was doing wrong was either using the same variable (i) for the add and remove or I had "ListBox2.RemoveItem (ListBox1.List(i))" instead of "ListBox2.AddItem (i)"
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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