|
本帖最后由 香川群子 于 2012-2-2 22:51 编辑
http://club.excelhome.net/thread-766196-1-1.html
我在这个帖子中,也有过尝试。
最后完成的对于VBA内存数组对象进行排序的代码如下:
需要排序的二维数组x(),数组总列数m,指定排序第n列,排序方式s(s=0时,Z-A排序,s=1时A-Z排序),
以及是否有标题行h,这样一共是4个参数
- Sub szpx(x(), m, n, s, h)
- For i = LBound(x) + h To UBound(x)
- If x(i, n) = "" Then p = p & "," & i
- Next
- If p <> "" Then
- y = x
- k = h
- For j = LBound(x) + h To UBound(x)
- If x(j, n) <> "" Then
- k = k + 1
- For l = 1 To m
- y(k, l) = x(j, l)
- Next
- End If
- Next
-
- q = Split(p, ",")
- b = UBound(q)
- For j = 1 To b
- k = k + 1
- For l = 1 To m
- y(k, l) = x(q(j), l)
- Next
- Next
- x = y
- End If
-
- For i = LBound(x) + h To UBound(x) - b
- t = x(i, n)
- p = "," & i
- For j = i + 1 To UBound(x) - b
- If s = 0 And t > x(j, n) Then
- t = x(j, n)
- p = "," & j
- ElseIf s = 1 And t < x(j, n) Then
- t = x(j, n)
- p = "," & j
- ElseIf t = x(j, n) Then
- p = p & "," & j
- End If
- Next
-
- q = Split(p, ",")
- c = UBound(q)
-
- y = x
- For j = 1 To c
- For l = 1 To m
- y(i + j - 1, l) = x(q(j), l)
- Next
- x(q(j), n) = ""
- Next
-
- k = i + c
- For j = i To UBound(x) - b
- If x(j, n) = "" Then
- If c = 1 Then Exit For Else c = c - 1
- Else
- For l = 1 To m
- y(k, l) = x(j, l)
- Next
- k = k + 1
- End If
- Next
-
- x = y
-
- Next
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|