+ Reply to Thread
Results 1 to 7 of 7

Double Listbox movement - Maintain Order

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    Office 2013
    Posts
    13

    Double Listbox movement - Maintain Order

    Hello everyone! This is my first time posting on ExcelForum, I am usually a lurker just checking stuff out - and usually it helps, but this time I'm truly stuck.

    Basically what I am doing is... I have 2 listboxes in a userform. With 4 buttons.

    Button One to move the selection (single or multiple items) from Listbox1 to Listbox2 and removing those items from Listbox1
    Button Two to move all list items from Listbox1 to Listbox2 and clearing Listbox1.
    Button Three to move the selection (single or multiple items) from Listbox2 to Listbox1 and removing those items from Listbox2.
    Button Four to move all list items from Listbox2 to Listbox1 and clearing Listbox2.

    The following is my code:

    Private Sub UserForm_Initialize()
    
    'Adds values to Listbox based on selected ranges.
    
    With ListBox1
        .AddItem Worksheets("Sheet1").Range("B12").Value
        .AddItem Worksheets("Sheet1").Range("B13").Value
        .AddItem Worksheets("Sheet1").Range("B14").Value
        .AddItem Worksheets("Sheet1").Range("B15").Value
        .AddItem Worksheets("Sheet1").Range("B16").Value
        .AddItem Worksheets("Sheet1").Range("B17").Value
        .AddItem Worksheets("Sheet1").Range("B18").Value
        .AddItem Worksheets("Sheet1").Range("B19").Value
        .AddItem Worksheets("Sheet1").Range("B20").Value
        .AddItem Worksheets("Sheet1").Range("B21").Value
        .AddItem Worksheets("Sheet1").Range("B22").Value
        .AddItem Worksheets("Sheet1").Range("B23").Value
        .AddItem Worksheets("Sheet1").Range("B24").Value
        .AddItem Worksheets("Sheet1").Range("B25").Value
        .AddItem Worksheets("Sheet1").Range("B26").Value
    End With
    
        ListBox1.MultiSelect = 2
        ListBox2.MultiSelect = 2
     
    End Sub
    What I'm having problems with, is the following:

    I want the order to remain as it is shown above. When moving back and forward between listboxes by pressing the button. Anyone know a solution to this without using a hidden sheet (i.e. only with coding)

    Private Sub CommandButton8_Click()
    Dim counter As Integer
    counter = 0
    
    'This code is used to add the selected item from the LEFT listbox to the one on the RIGHT
    
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
        ListBox2.AddItem ListBox1.List(i)
        End If
    Next i
    
    'This code below is used to remove the same selected item from the LEFT listbox, so there aren't 2 values of the same thing. And when
    'transfering back, doesn't cause repeats.
    'Counter is used to keep track of items that have been removed
    
    For q = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(q - counter) = True Then
            ListBox1.RemoveItem (q - counter)
            counter = counter + 1
        End If
    Next q
    
    End Sub
    
    
    Private Sub CommandButton9_Click()
    Dim counter2 As Integer
    counter2 = 0
    
    'Same as above, but from RIGHT listbox to LEFT
    
    For m = 0 To ListBox2.ListCount - 1
        If ListBox2.Selected(m) = True Then
        ListBox1.AddItem ListBox2.List(m)
        End If
    Next m
    
    For n = 0 To ListBox2.ListCount - 1
        If ListBox2.Selected(n - counter2) Then
            ListBox2.RemoveItem (n - counter2)
            counter2 = counter2 + 1
        End If
    Next n
    End Sub
    I have spent so long on this... I just can't seem to solve it. I have fixed it for transfering all, as that is pretty simple. Clear Listbox and additem again etc...

    Thanks guys, looking for help!

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Double Listbox movement - Maintain Order

    Hi and welcome to the forum.

    Try this...

    Private Sub UserForm_Initialize()
        
    'Adds values to Listbox based on selected ranges.
        
        ListBox1.List = Worksheets("Sheet1").Range("B12:B26").Value
        
        ListBox1.MultiSelect = fmMultiSelectMulti
        ListBox2.MultiSelect = fmMultiSelectMulti
     
    End Sub
    
    
    Private Sub CommandButton8_Click()
        
        'This code is used to add the selected item from the LEFT listbox to the one on the RIGHT
        
        Dim i As Long
        
        'Remove selected items from ListBox1
        If ListBox1.ListCount > 0 Then
            For i = ListBox1.ListCount - 1 To 0 Step -1
                If ListBox1.Selected(i) Then ListBox1.RemoveItem i
            Next i
        End If
        
        'Populate Listbox2 with all items.
        ListBox2.List = Worksheets("Sheet1").Range("B12:B26").Value
        'Remove remianing items in Listbox1 from Listbox2
        For i = ListBox2.ListCount - 1 To 0 Step -1
            If ListBox1.ListCount = 0 Then Exit For
            If IsNumeric(Application.Match(ListBox2.List(i), ListBox1.List, 0)) Then ListBox2.RemoveItem i
        Next i
        
    End Sub
    
    
    Private Sub CommandButton9_Click()
        
        'This code is used to add the selected item from the RIGHT listbox to the one on the LEFT
        
        Dim i As Long
        
        'Remove selected items from ListBox2
        If ListBox2.ListCount > 0 Then
            For i = ListBox2.ListCount - 1 To 0 Step -1
                If ListBox2.Selected(i) Then ListBox2.RemoveItem i
            Next i
        End If
        
        'Populate Listbox1 with all items.
        ListBox1.List = Worksheets("Sheet1").Range("B12:B26").Value
        'Remove remianing items in Listbox2 from Listbox1
        For i = ListBox1.ListCount - 1 To 0 Step -1
            If ListBox2.ListCount = 0 Then Exit For
            If IsNumeric(Application.Match(ListBox1.List(i), ListBox2.List, 0)) Then ListBox1.RemoveItem i
        Next i
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    07-20-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    Office 2013
    Posts
    13

    Re: Double Listbox movement - Maintain Order

    You are amazing . I had actually done a much more complicated thing haha. I got mine to work too, but yours makes much more sense. I basically did it step by step for what I wanted to happen.

    i.e.
    1. Select - Move selected
    2. Clear list 1
    3. Put all in List 1
    4. Select what's in List 2 in List 1
    5. Clear List 2
    6. Move stuff from list 1 to List 2
    7. Delete these "stuff" from list 1

    Haha. Well it works, thanks so much buddy . Just for your information, and not to lose information; my code:

    Private Sub CommandButton8_Click()
    Dim counter As Integer
    counter = 0
    
    'This code is used to add the selected item from the LEFT listbox to the one on the RIGHT
    
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
        ListBox2.AddItem ListBox1.List(i)
        End If
    Next i
    
    ListBox1.Clear
    
    ListBox1.List = Worksheets("Sheet1").Range("B12:B26").Value
    
    For t = 0 To ListBox2.ListCount - 1
        For r = 0 To ListBox1.ListCount - 1
            If ListBox1.List(r) = ListBox2.List(t) Then
                ListBox1.Selected(r) = True
            End If
        Next r
    Next t
    
    ListBox2.Clear
    
    For o = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(o) = True Then
        ListBox2.AddItem ListBox1.List(o)
        End If
    Next o
    
    'This code below is used to remove the same selected item from the LEFT listbox, so there aren't 2 values of the same thing. And when
    'transfering back, doesn't cause repeats.
    'Counter is used to keep track of items that have been removed
    
    For q = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(q - counter) = True Then
            ListBox1.RemoveItem (q - counter)
            counter = counter + 1
        End If
    Next q
    
    
    End Sub
    Last edited by Koncker; 07-29-2015 at 06:33 PM.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Double Listbox movement - Maintain Order

    You're welcome. Thanks for the feedback.

  5. #5
    Registered User
    Join Date
    07-20-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    Office 2013
    Posts
    13

    Re: Double Listbox movement - Maintain Order

    Hey AlphaFrog, I have one more question. Since you showed me how to add to list the range...

    ListBox1.List = Worksheets("Sheet1").Range("B12:B26").Value
    How would I do it if it my range was C2, D2, E2, ... , O2 and then Q2 - T2, V2, W2, Y2 - AA2 ?

    As in.. I have columns seperated in the middle. So I cant add the full range.

    Listbox1.List = Worksheets("Sheet1").Range("C2:O2").Value
    Listbox1.List = Worksheets("Sheet1").Range("Q2:T2").Value
    Listbox1.List = Worksheets("Sheet1").Range("V2:W2").Value
    Listbox1.List = Worksheets("Sheet1").Range("Y2:AA2").Value
    PS: I know this doesn't work as this just changes the list to what the new range values are... but I'm wondering how to add them all.

    This is what I currently have:

    With Listbox1
    
        .AddItem Worksheets("Sheet1").Range("C2").Value
        .AddItem Worksheets("Sheet1").Range("D2").Value
        .AddItem Worksheets("Sheet1").Range("E2").Value
        .AddItem Worksheets("Sheet1").Range("F2").Value
        .AddItem Worksheets("Sheet1").Range("G2").Value
        .AddItem Worksheets("Sheet1").Range("H2").Value
        .AddItem Worksheets("Sheet1").Range("I2").Value
        .AddItem Worksheets("Sheet1").Range("J2").Value
        .AddItem Worksheets("Sheet1").Range("K2").Value
        .AddItem Worksheets("Sheet1").Range("L2").Value
        .AddItem Worksheets("Sheet1").Range("M2").Value
        .AddItem Worksheets("Sheet1").Range("N2").Value
        .AddItem Worksheets("Sheet1").Range("O2").Value
        .AddItem Worksheets("Sheet1").Range("Q2").Value
        .AddItem Worksheets("Sheet1").Range("R2").Value
        .AddItem Worksheets("Sheet1").Range("S2").Value
        .AddItem Worksheets("Sheet1").Range("T2").Value
        .AddItem Worksheets("Sheet1").Range("V2").Value
        .AddItem Worksheets("Sheet1").Range("W2").Value
        .AddItem Worksheets("Sheet1").Range("Y2").Value
        .AddItem Worksheets("Sheet1").Range("Z2").Value
        .AddItem Worksheets("Sheet1").Range("AA2").Value
    
    End With
    Or is there a simpler way, Could I seperate the ranges via comma?
    Last edited by Koncker; 07-30-2015 at 05:10 AM.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Double Listbox movement - Maintain Order

        Dim cell As Range
        For Each cell In Worksheets("Sheet1").Range("C2:O2,Q2:T2,V2:W2,Y2:AA2")
            ListBox1.AddItem cell.Value
        Next cell

  7. #7
    Registered User
    Join Date
    07-20-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    Office 2013
    Posts
    13

    Re: Double Listbox movement - Maintain Order

    Thanks again AlphaFrog .

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Double Click Listbox question
    By frostii in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2015, 09:15 PM
  2. [SOLVED] Maintain selected listbox items when reopening worksheet (issue)
    By Platinum3x in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-09-2014, 11:47 PM
  3. Double click Listbox to edit item
    By johan12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2012, 04:12 AM
  4. Replies: 8
    Last Post: 08-15-2012, 02:42 AM
  5. Maintain double-click feature
    By rlarkin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2011, 07:09 AM
  6. userForm listBox being double-loaded
    By bstubbs in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-16-2009, 06:34 PM
  7. Need selected item from listbox after double click
    By peterfarge@hotmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2006, 11:20 AM

Tags for this Thread

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