Ok I get it.
Try this
Sub TestMySort()
Dim ary As Variant
ary = Array(20, 15, 2, 18, 17, 5, 11, 9, 1, 8, 14, 12)
ary = SortInGroups(ArrayToSort:=ary, NumInGroup:=4)
End Sub
Public Function SortInGroups(ArrayToSort As Variant, Optional NumInGroup As
Long = -1) As Variant
Dim aryToSort As Variant
Dim arySorted As Variant
Dim aryIndex As Long
Dim i As Long
If NumInGroup = -1 Then
SortInGroups = BubbleSort(ArrayToSort)
Else
ReDim arySorted(LBound(ArrayToSort) To UBound(ArrayToSort))
For i = LBound(ArrayToSort) To UBound(ArrayToSort) Step NumInGroup
ReDim aryToSort(1 To 4)
aryToSort(1) = ArrayToSort(LBound(ArrayToSort) + i)
aryToSort(2) = ArrayToSort(LBound(ArrayToSort) + i + 1)
aryToSort(3) = ArrayToSort(LBound(ArrayToSort) + i + 2)
aryToSort(4) = ArrayToSort(LBound(ArrayToSort) + i + 3)
aryToSort = BubbleSort(aryToSort)
arySorted(LBound(ArrayToSort) + i) = aryToSort(1)
arySorted(LBound(ArrayToSort) + i + 1) = aryToSort(2)
arySorted(LBound(ArrayToSort) + i + 2) = aryToSort(3)
arySorted(LBound(ArrayToSort) + i + 3) = aryToSort(4)
Next i
End If
SortInGroups = arySorted
End Function
Private Function BubbleSort(InVal As Variant, Optional Order As String =
"Asc") As Variant
Dim fChanges As Boolean
Dim iElement As Long
Dim iElement2 As Long
Dim temp As Variant
Dim ToSort
ToSort = InVal
Do
fChanges = False
For iElement = LBound(ToSort) To UBound(ToSort) - 1
If ((Order = "Asc" And ToSort(iElement) > ToSort(iElement + 1))
Or _
(Order <> "Asc" And ToSort(iElement) < ToSort(iElement +
1))) Then
'Swap elements
temp = ToSort(iElement)
ToSort(iElement) = ToSort(iElement + 1)
ToSort(iElement + 1) = temp
fChanges = True
End If
Next iElement
Loop Until Not fChanges
BubbleSort = ToSort
End Function
--
HTH
Bob
(there's no email, no snail mail, but somewhere should be gmail in my addy)