|
多列数组排序的话,排序自定义函数要换一个。
- Sub ykcbf2() '//2024.8.20 2维数组多列自定义排序
- r = Cells(Rows.Count, 1).End(3).Row
- arr = [a2].Resize(r - 1, 3)
- msort2 arr, 2, -1, 3, 1
- [k2].Resize(UBound(arr), UBound(arr, 2)) = arr
- End Sub
- Function msort2(arr, ParamArray sortParams() As Variant) As Variant
- Dim i As Integer, j As Integer
- Dim temp As Variant
- For i = LBound(arr, 1) To UBound(arr, 1) - 1
- For j = i + 1 To UBound(arr, 1)
- Dim compareResult As Integer
- compareResult = 0
- Dim k As Integer
- For k = LBound(sortParams) To UBound(sortParams) Step 2
- Dim column As Integer
- Dim sortOrder As Integer
- column = sortParams(k)
- sortOrder = sortParams(k + 1)
- If arr(i, column) > arr(j, column) Then
- compareResult = sortOrder
- Exit For
- ElseIf arr(i, column) < arr(j, column) Then
- compareResult = -sortOrder
- Exit For
- End If
- Next k
- If compareResult <> 0 Then
- If compareResult > 0 Then
- For k = LBound(arr, 2) To UBound(arr, 2)
- temp = arr(i, k)
- arr(i, k) = arr(j, k)
- arr(j, k) = temp
- Next k
- End If
- End If
- Next j
- Next i
- msort2 = arr
- End Function
复制代码
|
|