Userform ListBox customer ordering with VBA issue

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Hi


I have a couple of list boxes, Listbox1 is populated when the Userform is opened and the order is based on usability, higher usability at the top and lower at the bottom. Example code below

VBA Code:
With Me.ListBox1
            .AddItem "g1"
            .AddItem "b1"
            .AddItem "h1"
            .AddItem "d1"
            .AddItem "a1"
            .AddItem "f1"
            '.AddItem "e1
            .AddItem "c1"
    End With

Me.ListBox1.MultiSelect = fmMultiSelectMulti

I then have a command button in the UserForm to move selected items from ListBox1 to ListBox2, code below

Code:
Dim iCtr As Long
Dim V As Long

    For iCtr = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(iCtr) = True Then
            Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
        End If
    Next iCtr

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

    For V = ListBox2.ListCount - 1 To 0 Step -1
        ListBox2.ListIndex = V
    Next V

Finally I have a second command button in the UserForm to move selected items back from ListBox2 to ListBox1, then last part of the VB code in this button will order ListBox1 in alpha order, which I don’t want, I want it to apply the original order when populated, the only issue with that is not all items will be moved back from ListBox2 to ListBox1 so the original order needs to match with the exception of the missing items? Is this possible? A custom order sort? Code below to move items back

Code:
Dim iCtr As Long
    Dim K As Long
    Dim V As Long
    Dim Temp As Variant

    For iCtr = 0 To Me.ListBox2.ListCount - 1
        If Me.ListBox2.Selected(iCtr) = True Then
            Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
        End If
    Next iCtr

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

    With ListBox1
        For K = 0 To .ListCount - 2
            For V = 0 To .ListCount - 2
                If LCase(.List(V)) > LCase(.List(V + 1)) Then
                    Temp = .List(V)
                    .List(V) = .List(V + 1)
                    .List(V + 1) = Temp
                End If
            Next V
        Next K
    End With
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Michael

Why not have a second column in each listbox that indicates the original position of each item?

Both columns could be transferred between the listboxes as required and the second used for any sort of ordering that was needed.
 
Upvote 0
Yes, you can set the it's column width to 0 using the ColumnWidths property.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

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
Back
Top