+ Reply to Thread
Results 1 to 11 of 11

Remove selected item from listbox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379

    Remove selected item from listbox

    Sorry, should be a simple one...

    I can add an item from one list box to another using the following...

    Private Sub ListBox1_Click()
        ListBox2.AddItem ListBox1.Value
    End Sub
    But I want to be able to remove the item from the listbox by clicking it. Tried this but doesn't work!

    Private Sub ListBox2_Click()
         ListBox2.RemoveItem ListBox2.ListIndex
    End Sub
    Last edited by ChrisMattock; 12-04-2008 at 03:14 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    04-11-2006
    Posts
    407
    I would recommend you use the double-click over single-click since when you remove an item it'll reset the focus to act as if you single-clicked again. Here's my suggestion:
    Private Sub ListBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Dim i As Long
        For i = 0 To ListBox2.ListCount - 1
            If i > ListBox2.ListCount - 1 Then _
                Exit Sub
            If ListBox2.Selected(i) = True Then _
                ListBox2.RemoveItem (i)
        Next i
    End Sub

  3. #3
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379
    That's great - thanks for the help, one last thing, would it be possible to remove the item from the original ListBox when it is clicked, so it appears to move from one to the other? Then when it is removed it returns to the original? Thanks again for your help!

  4. #4
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379
    No worries have sorted it myself, thanks!

    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim i As Long
        ListBox2.AddItem ListBox1.Value
        For i = 0 To ListBox1.ListCount - 1
            If i > ListBox1.ListCount - 1 Then _
                Exit Sub
            If ListBox1.Selected(i) = True Then _
                ListBox1.RemoveItem (i)
        Next i
    End Sub
    
    
    Private Sub ListBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Dim i As Long
        ListBox1.AddItem ListBox2.Value
        For i = 0 To ListBox2.ListCount - 1
            If i > ListBox2.ListCount - 1 Then _
                Exit Sub
            If ListBox2.Selected(i) = True Then _
                ListBox2.RemoveItem (i)
        Next i
    End Sub

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480
    I know your solved but you may find this of interest.
    http://www.andypope.info/vba/listboxmover.htm
    Cheers
    Andy
    www.andypope.info

  6. #6
    Registered User
    Join Date
    01-21-2012
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Remove selected item from listbox

    I also trying to remove an item from a listbox. It works fine when a select any item except the last one listed.
    When I select the last item to remove all items in the listbox are removed

    here is the code

         For iCnt = Me.ListBox1.ListCount - 1 To 0 Step -1
            If Me.ListBox1.Selected(iCnt) = True Then
                Me.ListBox1.RemoveItem iCnt
            End If
        Next

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    I would not use the Click event.

    You can not use RowSource when you use RemoveItem of course.

    I would do this:
    Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
       If (ListBox1.Selected(ListBox1.ListIndex)) Then ListBox1.RemoveItem (ListBox1.ListIndex)
    End Sub
    
    Private Sub UserForm_Initialize()
      ListBox1.List = WorksheetFunction.Transpose(Range("A1:A10"))
      'ListBox1.RowSource = "A1:A10"
    End Sub
    
    Private Sub CommandButton1_Click()
      Unload Me
    End Sub

  8. #8
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    If you are going to use the same code for 2 or more listbox controls, you might want to use another sub.

    e.g.
    Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
      RemoveItem
    End Sub
    
    Private Sub RemoveItem()
      With ActiveControl
        If .Selected(.ListIndex) Then .RemoveItem .ListIndex
      End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379
    That's brilliant, thank you all so much for your help, I have what I need now! +rep to you all.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: Remove selected item from listbox

    prestopr, you should really start your own thread.

    If the Multiselect property is fmMultiSelectSingle then it will delete all items as the highlighed item will automatically become the last item in the list after deletion.
    When not the last item the highlighted item is that of the loop counter so does not get processed again.

    If selectsingle no need to loop.

  11. #11
    Registered User
    Join Date
    01-21-2012
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Remove selected item from listbox

    thanks very much for your help
    and I will start my owon thread next time

    cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1