|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
修改后的代码如下:
- Option Explicit
- Public Sub PaiXu1()
- Dim arr, n&, cs_max%, i&, j&, j1%, k&, k1%, m%, s1, s2, gd, jg, t#
- t = Timer
- n = Cells(Rows.Count, 1).End(xlUp).Row
- If n > 1 Then arr = Range("a1:a" & n).Value Else ReDim arr(1 To 1, 1 To 1): arr(1, 1) = Range("a1").Value
- jg = arr '记录原始结果
-
-
- ReDim cs(1 To n): cs_max = 0
- For i = 1 To n '自动检测最大层数
- cs(i) = Len(arr(i, 1)) - Len(Replace(arr(i, 1), "-", "")) + 1
- If cs_max < cs(i) Then cs_max = cs(i)
- Next i
-
-
- For i = 1 To n '统一编码层数
- m = cs_max - cs(i)
- arr(i, 1) = arr(i, 1) & String(m, "-")
- Next i
-
-
- ReDim ws_max(1 To cs_max), ws(1 To cs_max), brr(1 To n) '记录每一层位数最大值
- For i = 1 To cs_max '每一层位数最大值初始化
- ws_max(i) = 0
- Next i
- For i = 1 To n
- brr(i) = Split(arr(i, 1), "-")
- For j = 1 To cs_max
- ws(j) = Len(brr(i)(j - 1))
- If ws(j) > ws_max(j) Then ws_max(j) = ws(j)
- Next j
- Next i
-
-
- ReDim gsf$(1 To cs_max), arr(1 To n, 1 To 1) '生成格式符、清空arr数组
- For i = 1 To cs_max
- gsf(i) = String(ws_max(i), "0")
- Next i
- For i = 1 To n '统一每层位数并重新连接
- For j = 1 To cs_max
- arr(i, 1) = arr(i, 1) & Format(Val(brr(i)(j - 1)), gsf(j))
- Next j
- Next i
-
-
- For i = 1 To n - 1 '冒泡排序,一步到位
- For j = i + 1 To n
- If Val(arr(j, 1)) < Val(arr(i, 1)) Then
- gd = jg(j, 1): jg(j, 1) = jg(i, 1): jg(i, 1) = gd
- gd = arr(j, 1): arr(j, 1) = arr(i, 1): arr(i, 1) = gd
- End If
- Next j
- Next i
- Range("c:c").ClearContents: Range("c1").Resize(n) = jg: Range("d1") = "用时:" & Format(Timer - t, "0.0000") & "秒。"
- End Sub
复制代码 可以自动检测层数,及每层位数,适应性强,效率比前次排序高。
|
|