We help IT Professionals succeed at work.

# Quicksort for multiple columns in excel vba userform

on
2,873 Views
I have a multicolumn listbox that I want to have the option of sorting any of the 7 columns. I would use the bubblesort, but there can be potentially 35000 in the list which makes it kind of slow. I found the code below for quicksort, but I am not sure how I can modify it so that I can sort multiple columns, not just one. Can anyone help me?

Sub Quicksort (List() As Long, min As Integer, max As Integer)
Dim med_value As Long
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

' If the list has no more than 1 element, it's sorted.
If min >= max Then Exit Sub

' Pick a dividing item.
i = Int((max - min + 1) * Rnd + min)
med_value = List(i)

' Swap it to the front so we can find it easily.
List(i) = List(min)

' Move the items smaller than this into the left
' half of the list. Move the others into the right.
lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While List(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
List(lo) = med_value
Exit Do
End If

' Swap the lo and hi values.
List(lo) = List(hi)

' Look up from lo for a value >= med_value.
lo = lo + 1
Do While List(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
List(hi) = med_value
Exit Do
End If

' Swap the lo and hi values.
List(hi) = List(lo)
Loop

' Sort the two sublists
Quicksort List(), min, lo - 1
Quicksort List(), lo + 1, max
End Sub
Comment
Watch Question
Commented:
This problem has been solved!
###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant