標題:
[發問]
VBA 字數排序
[打印本頁]
作者:
s13030029
時間:
2019-5-22 10:46
標題:
VBA 字數排序
請問VBA有辦法依中文字數多寡做排序嗎?
要怎麼寫呢?
作者:
Scott090
時間:
2019-5-23 20:08
回復
2#
s13030029
上傳一個工作表舉個例 應該會比較容易理解
作者:
stillfish00
時間:
2019-5-24 14:42
回復
1#
s13030029
Public Sub MergeSort(array_in, begin, final, Optional array_temp)
'array_in: 1 dimension array
If begin >= final Then Exit Sub
If IsMissing(array_temp) Then
ReDim array_temp(LBound(array_in) To UBound(array_in))
End If
Dim mid
mid = (begin + final) \ 2
MergeSort array_in, begin, mid, array_temp
MergeSort array_in, mid + 1, final, array_temp
Merge array_in, begin, mid, final, array_temp
End Sub
Private Sub Merge(ar, begin, mid, final, temp)
Dim left: left = begin
Dim right: right = mid + 1
Dim i: i = begin
Do
If left > mid Then Exit Do
If right > final Then Exit Do
If CmprOperator(ar(left), ar(right)) Then
temp(i) = ar(left)
left = left + 1
Else
temp(i) = ar(right)
right = right + 1
End If
i = i + 1
Loop
For n = left To mid
temp(i) = ar(n)
i = i + 1
Next
For n = right To final
temp(i) = ar(n)
i = i + 1
Next
For n = begin To final
ar(n) = temp(n)
Next
End Sub
Private Function CmprOperator(a, b) As Boolean
If Len(a) <= Len(b) Then
CmprOperator = True
Else
CmprOperator = False
End If
End Function
複製代碼
Public Sub test()
Dim ar
ar = Array("請問", "VBA", "有辦法", "依", "中文字數多寡", "做", "排序", "嗎")
MergeSort ar, LBound(ar), UBound(ar)
Debug.Print Join(ar, ",")
' Output>> 依,做,嗎,請問,排序,VBA,有辦法,中文字數多寡
End Sub
複製代碼
作者:
s13030029
時間:
2019-5-28 13:53
回復
3#
Scott090
回復
4#
stillfish00
依照C欄儀器做排序
[attach]30697[/attach]
作者:
Scott090
時間:
2019-5-28 15:57
回復
4#
s13030029
[attach]30701[/attach]
請測試是不是你要的
作者:
s13030029
時間:
2019-5-28 16:04
回復
5#
Scott090
謝謝~這是我要的~
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)