|
楼主 |
发表于 2016-1-9 11:56
|
显示全部楼层
2、数组排序函数
VBA没有为我们提供数组排序功能,这是一个缺憾。实际开发中,人们或者书写冗长的排序语句,或者干脆用单元格排序语句来变通代替,也可勉强解决问题,二者的弊端显而易见。此函数可实现最多三个关键字的排序,基本满足实战需要。如有更多关键字排序需求的,可根据此代码思路,进一步深化函数的功能。
Public Function sortarr(arr, key1, order1, Optional key2 = 0, Optional order2 = 1, Optional key3 = 0, Optional order3 = 1)
Rem 数组排序函数
Rem arr为被排序数组,含key参数为排序字段,含order参数为排序次序,key和order两两一组,最多三组,最多可对三个字段排序,后两组排序参数可省略
Dim i, ii, c, code, tmp, code1, code2, tmparr(), v1, v2, v3, v4, v5, v6, tmp0, tmp1, tmp2, tmp3, tmp4
If LBound(arr) = 0 Then
Rem 一维数组排序
If order1 = 1 Then code = "<" Else code = ">"
For i = 0 To UBound(arr) - 1
For ii = i + 1 To UBound(arr)
tmp = Evaluate(arr(ii) & code & arr(i))
If tmp = True Then tmp1 = arr(i): arr(i) = arr(ii): arr(ii) = tmp1
Next
Next
Else
Rem 二维数组排序
ReDim tmparr(1 To UBound(arr, 2))
If key2 = 0 And key3 = 0 Then
Rem 一个关键字排序
If order1 = 1 Then code1 = "<" Else code1 = ">"
For i = 1 To UBound(arr) - 1
For ii = i + 1 To UBound(arr)
v1 = arr(ii, key1): v2 = arr(i, key1)
'evaluate括号中的字符串两面要加引号
If TypeName(arr(ii, key1)) = "String" Then v1 = """" & arr(ii, key1) & """"
If TypeName(arr(i, key1)) = "String" Then v2 = """" & arr(i, key1) & """"
tmp = Evaluate(v1 & code1 & v2)
If tmp = True Then
For c = 1 To UBound(arr, 2): tmparr(c) = arr(i, c): Next
For c = 1 To UBound(arr, 2): arr(i, c) = arr(ii, c): Next
For c = 1 To UBound(arr, 2): arr(ii, c) = tmparr(c): Next
End If
Next
Next
ElseIf key2 <> 0 And key3 = 0 Then
Rem 两个关键字排序
If order1 = 1 Then code1 = "<" Else code1 = ">"
If order2 = 1 Then code2 = "<" Else code2 = ">"
For i = 1 To UBound(arr) - 1
For ii = i + 1 To UBound(arr)
v1 = arr(ii, key1): v2 = arr(i, key1)
If TypeName(arr(ii, key1)) = "String" Then v1 = """" & arr(ii, key1) & """"
If TypeName(arr(i, key1)) = "String" Then v2 = """" & arr(i, key1) & """"
tmp1 = Evaluate(v1 & code1 & v2)
tmp0 = Evaluate(v1 & "=" & v2)
v3 = arr(ii, key2): v4 = arr(i, key2)
If TypeName(arr(ii, key2)) = "String" Then v3 = """" & arr(ii, key2) & """"
If TypeName(arr(i, key2)) = "String" Then v4 = """" & arr(i, key2) & """"
tmp2 = Evaluate(v3 & code2 & v4)
If tmp1 = True Or (tmp0 = True And tmp2 = True) Then
For c = 1 To UBound(arr, 2): tmparr(c) = arr(i, c): Next
For c = 1 To UBound(arr, 2)
arr(i, c) = arr(ii, c)
Next
For c = 1 To UBound(arr, 2)
arr(ii, c) = tmparr(c)
Next
End If
Next
Next
ElseIf key2 <> 0 And key3 <> 0 Then
Rem 三个关键字排序
If order1 = 1 Then code1 = "<" Else code1 = ">"
If order2 = 1 Then code2 = "<" Else code2 = ">"
If order3 = 1 Then code3 = "<" Else code3 = ">"
For i = 1 To UBound(arr) - 1
For ii = i + 1 To UBound(arr)
v1 = arr(ii, key1): v2 = arr(i, key1)
If TypeName(arr(ii, key1)) = "String" Then v1 = """" & arr(ii, key1) & """"
If TypeName(arr(i, key1)) = "String" Then v2 = """" & arr(i, key1) & """"
tmp1 = Evaluate(v1 & code1 & v2)
tmp0 = Evaluate(v1 & "=" & v2)
v3 = arr(ii, key2): v4 = arr(i, key2)
If TypeName(arr(ii, key2)) = "String" Then v3 = """" & arr(ii, key2) & """"
If TypeName(arr(i, key2)) = "String" Then v4 = """" & arr(i, key2) & """"
tmp2 = Evaluate(v3 & "=" & v4)
tmp3 = Evaluate(v3 & code2 & v4)
v5 = arr(ii, key3): v6 = arr(i, key3)
If TypeName(arr(ii, key3)) = "String" Then v5 = """" & arr(ii, key3) & """"
If TypeName(arr(i, key3)) = "String" Then v6 = """" & arr(i, key3) & """"
tmp4 = Evaluate(v5 & code2 & v6)
If tmp1 = True Or (tmp0 = True And tmp3 = True) Or (tmp0 = True And tmp2 = True And tmp4 = True) Then
For c = 1 To UBound(arr, 2): tmparr(c) = arr(i, c): Next
For c = 1 To UBound(arr, 2)
arr(i, c) = arr(ii, c)
Next
For c = 1 To UBound(arr, 2)
arr(ii, c) = tmparr(c)
Next
End If
Next
Next
End If
End If
sortarr = arr
End Function |
|