sort listbox by 2nd column

wCJanssen

New Member
Joined
Feb 22, 2009
Messages
24
Hi there,

I've found the following code elsewhere; it sorts a multicolumn listbox in ascending order, based on the values in column 1. It works fine, but I'd like the code to be adapted so that it sorts the listbox based on the values in column 2. I´d be very happy if anyone could help me with this. Thanks in advance.

Code:
Private Sub SorteerNaam_Click()
    Dim i As Long
    Dim j As Long
    Dim sTemp As String
    Dim sTemp2 As String
    Dim LbList As Variant
 
    LbList = Me.Overzicht.List
    For i = LBound(LbList, 1) To UBound(LbList, 1) - 1
        For j = i + 1 To UBound(LbList, 1)
            If LbList(i, 0) > LbList(j, 0) Then
 
                sTemp = LbList(i, 0)
                LbList(i, 0) = LbList(j, 0)
                LbList(j, 0) = sTemp
 
                sTemp2 = LbList(i, 1)
                LbList(i, 1) = LbList(j, 1)
                LbList(j, 1) = sTemp2
 
                sTemp3 = LbList(i, 2)
                LbList(i, 2) = LbList(j, 2)
                LbList(j, 2) = sTemp3
 
                sTemp4 = LbList(i, 3)
                LbList(i, 3) = LbList(j, 3)
                LbList(j, 3) = sTemp4
 
                sTemp5 = LbList(i, 4)
                LbList(i, 4) = LbList(j, 4)
                LbList(j, 4) = sTemp5
 
                sTemp6 = LbList(i, 5)
                LbList(i, 5) = LbList(j, 5)
                LbList(j, 5) = sTemp6
 
            End If
        Next j
    Next i
    Me.Overzicht.Clear
 
    Me.Overzicht.List = LbList
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi, Try this:-

Code:
Private Sub SorteerNaam_Click()
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] j [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] sTemp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] sTemp2 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] sTemp3 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] sTemp4 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] sTemp5 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] sTemp6 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] LbList [COLOR="Navy"]As[/COLOR] Variant
'[COLOR="Green"][B][/B][/COLOR]
    LbList = Me.Overzicht.List
    [COLOR="Navy"]For[/COLOR] i = LBound(LbList, 1) To UBound(LbList, 1)
        [COLOR="Navy"]For[/COLOR] j = i + 1 To UBound(LbList, 1)
            [COLOR="Navy"]If[/COLOR] LbList(i, 1) > LbList(j, 1) [COLOR="Navy"]Then[/COLOR]
 
                sTemp = LbList(i, 0)
                LbList(i, 0) = LbList(j, 0)
                LbList(j, 0) = sTemp
 
                sTemp2 = LbList(i, 1)
                LbList(i, 1) = LbList(j, 1)
                LbList(j, 1) = sTemp2
 
                sTemp3 = LbList(i, 2)
                LbList(i, 2) = LbList(j, 2)
                LbList(j, 2) = sTemp3
 
                sTemp4 = LbList(i, 3)
                LbList(i, 3) = LbList(j, 3)
                LbList(j, 3) = sTemp4
 
                sTemp5 = LbList(i, 4)
                LbList(i, 4) = LbList(j, 4)
                LbList(j, 4) = sTemp5
 
                sTemp6 = LbList(i, 5)
                LbList(i, 5) = LbList(j, 5)
                LbList(j, 5) = sTemp6
 
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] j
    [COLOR="Navy"]Next[/COLOR] i
    
    Me.Overzicht.Clear
 
    Me.Overzicht.List = LbList
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Change:

If LbList(i, 0) > LbList(j, 0) Then

To:

If LbList(i, 1) > LbList(j, 1) Then

Also, the lines which swap values in each column can be reduced to a loop.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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