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
 

Some videos you may like

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.

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)"
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,760
Messages
5,513,239
Members
408,942
Latest member
SayHayKid

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top