Sorting a listbox

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
I found this code and it works great to sort by the first column of my 4 column list box but I want the open to sort by the other columns. I will have the user click the label above the column so I only need to know what to change to indicate sorting by column 1 2 3 (default column 0 as listbox columns start with 0)

Code:
Private Sub Label3_Click() ' sort list box by quote number
 'Sorts ListBox List
     Dim i As Long, j As Long, x As Long, sTemp As String
    With mainform.searchbox
        For j = LBound(.List) To UBound(.List) - 1 Step 1
            For i = LBound(.List) To UBound(.List) - 1 Step 1
                If .List(i) > .List(i + 1) Then
                    [COLOR=#ff0000]For x = 0 To (.ColumnCount - 1) Step 1
[/COLOR]                        sTemp = .List(i, x)
                        .List(i, x) = .List(i + 1, x)
                        .List(i + 1, x) = sTemp
                    Next x
                End If
            Next i
        Next j
    End With
End Sub

I thought changing the zero in red would do it, but nope
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
i hope am right about this :) Try

Since the second argument in .List is left out, it assumes column 1, so i have included the second argument to be P. So P=1 is 2nd column, P=2, 3rd column.....

o all you need to be able to do is return an integer depending on which label was clicked(select case maybe) and feed that into the code
Rich (BB code):
Private Sub Label3_Click() ' sort list box by quote number
 'Sorts ListBox List
     Dim i As Long, j As Long, x As Long, sTemp As String, P As Long
     P = 1 'sorts using column 2
    With mainform.searchbox
        For j = LBound(.List) To UBound(.List) - 1 Step 1
            For i = LBound(.List) To UBound(.List) - 1 Step 1
                If .List(i, P) > .List(i + 1, P) Then
                    For x = 0 To (.ColumnCount - 1) Step 1
                        sTemp = .List(i, x)
                        .List(i, x) = .List(i + 1, x)
                        .List(i + 1, x) = sTemp
                    Next x
                End If
            Next i
        Next j
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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