|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 cgzn 于 2024-8-1 15:26 编辑
Option Compare Text '放模块最顶部
'排序规则SortRule的格式为"1,-2,3"...,其中-2的-表示降序排列,2表示对arr的2列排序,顺序从1-3列依次排序
Sub ColsPX(arr, SortRule, Optional lt = -1, Optional rt = -1, Optional px = 0)
If lt = -1 Then lt = LBound(arr)
If rt = -1 Then rt = UBound(arr)
If px = 0 Then MergSort1 arr, SortRule, lt, rt: px = 1
pxr = Split(SortRule, ","): j1 = pxr(px - 1): j = Abs(j1): If px <= UBound(pxr) Then j0 = pxr(px)
If lt < rt And px <= UBound(pxr) Then
For i = lt + 1 To rt
If arr(i, j) = arr(i - 1, j) Then n = n + 1 Else n0 = n: n = 0
If n0 > 0 And n = 0 Then
rtn = i - 1: ltn = i - 1 - n0: MergSort1 arr, j0, ltn, rtn: ColsPX arr, SortRule, ltn, rtn, px + 1
ElseIf i = rt And n > 0 Then rtn = rt: ltn = i - n: MergSort1 arr, j0, ltn, rtn: ColsPX arr, SortRule, ltn, rtn, px + 1
End If
Next
End If
End Sub
Sub MergSort1(arr, SortRule, Optional lt = -1, Optional rt = -1)
pxr = Split(SortRule, ","): subsize = 1: Dim tmprr(): Dim jd As Long: jd = pxr(0)
If lt = -1 Then lt = LBound(arr)
If rt = -1 Then rt = UBound(arr)
ci = LBound(arr, 2): cend = UBound(arr, 2): ReDim tmprr(lt To rt, ci To cend)
While subsize < rt - lt + 1
lf = lt
While lf < rt
md = Application.Min(lf + subsize - 1, rt): rg = Application.Min(lf + 2 * subsize - 1, rt)
For i = lf To rg
For j = ci To cend
tmprr(i, j) = arr(i, j)
Next
Next
i = lf: j = md + 1: k = lf
While i <= md And j <= rg
If tmprr(i, Abs(jd)) < tmprr(j, Abs(jd)) And jd > 0 Or tmprr(i, Abs(jd)) > tmprr(j, Abs(jd)) And jd < 0 Then
2: arr(k, ci) = tmprr(i, ci): If ci < cend Then ci = ci + 1: GoTo 2 Else ci = LBound(arr, 2)
i = i + 1
Else
7: arr(k, ci) = tmprr(j, ci): If ci < cend Then ci = ci + 1: GoTo 7 Else ci = LBound(arr, 2)
j = j + 1
End If
k = k + 1
Wend
While i <= md
5: arr(k, ci) = tmprr(i, ci): If ci < cend Then ci = ci + 1: GoTo 5 Else ci = LBound(arr, 2)
i = i + 1: k = k + 1
Wend
While j <= rg
6: arr(k, ci) = tmprr(j, ci): If ci < cend Then ci = ci + 1: GoTo 6 Else ci = LBound(arr, 2)
j = j + 1: k = k + 1
Wend
lf = lf + 2 * subsize
Wend
subsize = subsize * 2
Wend
End Sub
|
|