Hi all,
I have a multicolumn listbox in a userform that has 5 columns. As my dataset is growing, I need to allow users to sort the columns using command buttons placed above each column. I have found examples of how to sort 1 or 2 columns, but cannot find anything for 5 columns.
The following is what I have used for a 2-column sort, and am curious if it can be modified to be used for a 5-colum sort.
Dim i As Long
Dim j As Long
Dim sTemp As String
Dim sTemp2 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, 0) > LbList(j, 0) 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
End If
Next j
Next i
'Remove the contents of the listbox
Me.ListBox1.Clear
'Repopulate with the sorted list
Me.ListBox1.List = LbList
Bookmarks