EmptyVessel
09-11-2009, 04:02 PM
There are probably other versions of this type of sub routine but here it is for your pleasure.
Features:
1) Accepts a Listbox or Combobox
2) Sorts Ascending and Descending
3) Sorts Case Sensitive or not
4) Sorts a Given Range of Items. (Sort separate list segments.)
5) Sorts Numerically or Not or Mixed Numerical and Alpha
6) Sorts Numerically (If the Item has a number prefix followed by a space)
Example feature 6
1. ___ English
2. ___ Spanish
10. __ German
24. __ Italian
101. _ French
Example of feature 5
1
3
16
22
Bird
Cat
Dog
Example of feature 3
...
--- Fruits ---
Apple
Banana
Pear
--- Spices ---
Cinnimon
Clove
Nutmeg
Thyme
--- Juice ---
Apple
Cranberry
Orange
This utility is not meant to be used on really large lists.
needless to say it also works for VBA's list controls
Have a ball. :D
Private Sub SortRange(ByRef LST As Control, _
Optional ByVal SortDescending As Boolean = False, _
Optional ByVal Numerical As Boolean = False, _
Optional ByVal IgnoreCase As Boolean = False, _
Optional ByVal StartRange As Integer = 0, _
Optional ByVal EndRange As Integer = -1)
Dim CompareMethod As VbCompareMethod
Dim s1() As String
Dim s2() As String
Dim Hold As String
Dim Num1 As String
Dim Num2 As String
Dim Result As Integer
Dim i As Integer
Dim Sorted As Boolean
If Not (TypeName(LST) = "ListBox" Or TypeName(LST) = "ComboBox") Then Exit Sub
If LST.ListCount < 1 Then Exit Sub
If IgnoreCase Then
CompareMethod = vbTextCompare
Else
CompareMethod = vbBinaryCompare
End If
If EndRange = -1 Then EndRange = LST.ListCount - 1
Sorted = False
With LST
Do While Not Sorted
Sorted = True
For i = StartRange To EndRange - 1
If Numerical Then
s1 = Split(.List(i), " ")
Num1 = Trim(s1(0))
s2 = Split(.List(i + 1), " ")
Num2 = Trim(s2(0))
If IsNumeric(Num1) And IsNumeric(Num2) Then
If CDbl(Num1) > CDbl(Num2) Then
Result = 1
Else
Result = -1
End If
Else
Result = StrComp(.List(i), .List(i + 1), CompareMethod)
End If
Else
Result = StrComp(.List(i), .List(i + 1), CompareMethod)
End If
If SortDescending Then
If Result < 0 Then
Hold = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = Hold
Sorted = False
Exit For
End If
Else
If Result > 0 Then
Hold = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = Hold
Sorted = False
Exit For
End If
End If
Next i
Loop
End With
End Sub
Features:
1) Accepts a Listbox or Combobox
2) Sorts Ascending and Descending
3) Sorts Case Sensitive or not
4) Sorts a Given Range of Items. (Sort separate list segments.)
5) Sorts Numerically or Not or Mixed Numerical and Alpha
6) Sorts Numerically (If the Item has a number prefix followed by a space)
Example feature 6
1. ___ English
2. ___ Spanish
10. __ German
24. __ Italian
101. _ French
Example of feature 5
1
3
16
22
Bird
Cat
Dog
Example of feature 3
...
--- Fruits ---
Apple
Banana
Pear
--- Spices ---
Cinnimon
Clove
Nutmeg
Thyme
--- Juice ---
Apple
Cranberry
Orange
This utility is not meant to be used on really large lists.
needless to say it also works for VBA's list controls
Have a ball. :D
Private Sub SortRange(ByRef LST As Control, _
Optional ByVal SortDescending As Boolean = False, _
Optional ByVal Numerical As Boolean = False, _
Optional ByVal IgnoreCase As Boolean = False, _
Optional ByVal StartRange As Integer = 0, _
Optional ByVal EndRange As Integer = -1)
Dim CompareMethod As VbCompareMethod
Dim s1() As String
Dim s2() As String
Dim Hold As String
Dim Num1 As String
Dim Num2 As String
Dim Result As Integer
Dim i As Integer
Dim Sorted As Boolean
If Not (TypeName(LST) = "ListBox" Or TypeName(LST) = "ComboBox") Then Exit Sub
If LST.ListCount < 1 Then Exit Sub
If IgnoreCase Then
CompareMethod = vbTextCompare
Else
CompareMethod = vbBinaryCompare
End If
If EndRange = -1 Then EndRange = LST.ListCount - 1
Sorted = False
With LST
Do While Not Sorted
Sorted = True
For i = StartRange To EndRange - 1
If Numerical Then
s1 = Split(.List(i), " ")
Num1 = Trim(s1(0))
s2 = Split(.List(i + 1), " ")
Num2 = Trim(s2(0))
If IsNumeric(Num1) And IsNumeric(Num2) Then
If CDbl(Num1) > CDbl(Num2) Then
Result = 1
Else
Result = -1
End If
Else
Result = StrComp(.List(i), .List(i + 1), CompareMethod)
End If
Else
Result = StrComp(.List(i), .List(i + 1), CompareMethod)
End If
If SortDescending Then
If Result < 0 Then
Hold = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = Hold
Sorted = False
Exit For
End If
Else
If Result > 0 Then
Hold = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = Hold
Sorted = False
Exit For
End If
End If
Next i
Loop
End With
End Sub