Board logo

標題: [發問] 如何排序英數字? [打印本頁]

作者: sammy360    時間: 2015-1-14 18:21     標題: 如何排序英數字?

請問有辦法排序英數字的資料嗎?
例如:
排序前
a1, b1,a3, ab1, aa3,aa1,

排序後
a1, a3, b1, aa1, aa3, ab1

請前備們不吝賜教!
作者: bobomi    時間: 2015-1-15 13:12

本帖最後由 bobomi 於 2015-1-15 13:22 編輯

Sub XX()


ar = Array("a1", "b1", "a3", "ab1", "aa3", "aa1")

s = ar

For w = 0 To UBound(ar)
    s(w) = ""
    For k = 1 To Len(ar(w))
        s(w) = s(w) & Right("0000" & AscW(Mid(ar(w), k, 1)), 4)
    Next
    If Len(s(w)) > m Then m = Len(s(w))
Next
For w = 0 To UBound(ar)
    s(w) = Right(Space(m) & s(w), m)
Next

For i = 0 To UBound(ar)
    For j = 0 To UBound(ar) - 1
     If (s(j)) > (s(j + 1)) Then
     temp = s(j)
     s(j) = s(j + 1)
     s(j + 1) = temp
     
     temp = ar(j)
     ar(j) = ar(j + 1)
     ar(j + 1) = temp
     End If
Next j
Next i


MsgBox Join(ar, vbCrLf)

End Sub
作者: bobomi    時間: 2015-1-15 13:43

改成這各囉

Sub XX()


ar = Array("a1", "b1", "a3", "ab1", "aa3", "aa1", "aa0")

For w = 0 To UBound(ar)
    If Len(ar(w)) > m Then m = Len(ar(w))
Next
For w = 0 To UBound(ar)
    ar(w) = Right(Space(m) & ar(w), m)
Next

For i = 0 To UBound(ar)
    For j = 0 To UBound(ar) - 1
     If (ar(j)) > (ar(j + 1)) Then
     
     temp = ar(j)
     ar(j) = ar(j + 1)
     ar(j + 1) = temp
     End If
Next j
Next i
For w = 0 To UBound(ar)
    ar(w) = LTrim(ar(w))
Next


MsgBox Join(ar, vbCrLf)

End Sub
作者: sammy360    時間: 2015-1-16 17:59

回復 3# bobomi

好的,我再來試試看,謝謝bobomi大大!
作者: sammy360    時間: 2015-3-23 16:28

回復 3# bobomi

請問bobomi大大
我將儲存格資料丟入陣列後,為什麼無法用你所提供的程式排序?

ex:
Dim ar
ar = Range("a1:a8")




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)