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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

rwheeler

Board Regular
Joined
Apr 22, 2010
Messages
105
I want to have the ability to remove certain items, not clear the whole thing
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Untested

Code:
With UserForm1.textbox2
 .AddItem ""
 .ListIndex = .ListCount - 1
 .RemoveItem .ListCount - 1
End With
 

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582

ADVERTISEMENT

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:

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
..Which one worked? lol
I have no idea how VoG's code works from just seeing it o_O
 

rwheeler

Board Regular
Joined
Apr 22, 2010
Messages
105
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)"
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,400
Messages
5,831,413
Members
430,066
Latest member
rajd72

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
Top