Home Excel VB/VBA Sort Listbox by Column
20 | 05 | 2012
This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade

Sort Listbox by Column PDF Print E-mail
User Rating: / 1
PoorBest 
Tutorials

You can use the code below to sort a listbox by column. The code below is based on a listbox called Listbox1 that holds 2 columns. You can expand if you like. Just follow the same code build up as stated below. 

[code] 

Private Sub Sorting(ByVal column As Double)
On Error Resume Next
If Me.ListBox1.ListCount = 0 Then Exit Sub
    Dim i As Long
    Dim j As Long
    Dim sTemp As String
    Dim sTemp2 As String
    'Dim sTemp3 As String
    'Dim sTemp4 As String
    'Dim sTemp........ As String
    Dim LbList As Variant
    'Store the list in an array for sorting
    LbList = Me.ListBox1.List
    'Bubble sort the array on the first value
    For i = LBound(LbList, 1) To UBound(LbList, 1) - 1
        For j = i + 1 To UBound(LbList, 1)
            If LbList(i, column) > LbList(j, column) Then
                'Swap the first value
                sTemp = LbList(i, 0)
                LbList(i, 0) = LbList(j, 0)
                LbList(j, 0) = sTemp
                'Swap the second value
                sTemp2 = LbList(i, 1)
                LbList(i, 1) = LbList(j, 1)
                LbList(j, 1) = sTemp2
                'Swap the second value
                'sTemp3 = LbList(i, 2)
                'LbList(i, 2) = LbList(j, 2)
                'LbList(j, 2) = sTemp3
                'Swap the second value
                'sTemp4 = LbList(i, 3)
                'LbList(i, 3) = LbList(j, 3)
                'LbList(j, 3) = sTemp4
                '.........
                '.....
                '...
            End If
        Next j
    Next i
    'Remove the contents of the listbox
    Me.ListBox1.Clear
    'Repopulate with the sorted list
    Me.ListBox1.List = LbList
End Sub
[/code]
Attachments:
FileDescriptionFile sizeDownloads
Download this file (SortColumn.xls)SortColumnSort Listbox by Column Sample File26 Kb132
 

Add comment


Security code
Refresh

This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade