Consulting

Results 1 to 5 of 5

Thread: Moving Items Between ListBoxes

  1. #1
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location

    Moving Items Between ListBoxes

    I have two list boxes in a UserForm: lstbxA and lstbxB. Initially, lstbxA is filled with some items; lstbxB is blank. I wish to move an item, say "X" from lstbxA to lstbxB. That process is pretty obvious. But in doing so, I wish to "remove "X" from lstbxA so it is no longer a choice. That is not so obvious. I can loop through the items in lstbxA, find "X" and then delete that item. That seems cumbersome. Is there a way to directly delete "X" from lstbxA?
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Are you moving multiple items?

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Try
    [VBA]
    Private Sub UserForm_Initialize()
    For i = 1 To 10
    ListBox1.AddItem "Data" & i
    Next
    End Sub
    Private Sub CommandButton1_Click()
    Dim tmp As Long
    tmp = ListBox1.ListIndex
    ListBox2.AddItem ListBox1
    ListBox1.RemoveItem (tmp)
    End Sub

    [/VBA]

    or

    http://support.microsoft.com/kb/213756/EN-US if list comes from your spreadsheet.

    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by mdmackillop
    Try
    [vba]
    Private Sub UserForm_Initialize()
    For i = 1 To 10
    ListBox1.AddItem "Data" & i
    Next
    End Sub
    Private Sub CommandButton1_Click()
    Dim tmp As Long
    tmp = ListBox1.ListIndex
    ListBox2.AddItem ListBox1
    ListBox1.RemoveItem (tmp)
    End Sub

    [/vba]
    http://support.microsoft.com/kb/213756/EN-US if list comes from your spreadsheet.

    Regards
    MD
    Thanks. Seems so obvious when shown ...

    So, after moving items back a forth, I want to sort (the items in) both ListBoxes. Again, I know how to do this via brute force, using, say, a bubble sort, but is there an elegant way? perhaps something built into the ListBox properties or methods
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Can't think how to achieve this without an "external" sort. You could write/read the listbox items to a hidden sheet and use dynamic ranges and worksheet sorts, but I'm not sure that is any more elegant.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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