標題:
[發問]
Bubblesort 的迷思在文字+數值格式時?
[打印本頁]
作者:
ui123
時間:
2015-6-21 12:54
標題:
Bubblesort 的迷思在文字+數值格式時?
各位大大,這個問題我查了蠻久了但始終沒辦法單用VBA解決(目前用split然後再由後面數值用Bubblesort),功力不夠...
如果單純用Bubblesort排出來像醬:
javascript:;javascript:;
常常遇到 文字+"-"或"/"或"#" 的格式,困擾蠻久的,有大大也遇過這種問題嗎? 麻煩提點一下,感激不盡^^
作者:
no3-taco
時間:
2015-7-1 10:13
回復
1#
ui123
給你參考看看
Sub ex() '格式化文字,將數字變成固定長度文字補00
Debug.Print Format("62", "0000")
Debug.Print Format("00062", "####")
End Sub
複製代碼
作者:
lpk187
時間:
2015-7-1 11:51
回復
1#
ui123
先文字排序,再數字排序
應該可以達成你要的效果!
Sub Bubblesort()
Dim i, j As Integer
Dim arr, si, sj, s As Variant
arr = Range("a4:a" & Cells(Rows.Count, 1).End(xlUp).Row)
For i = 1 To UBound(arr)
For j = i + 1 To UBound(arr)
If arr(i, 1) > arr(j, 1) Then
s = arr(i, 1)
arr(i, 1) = arr(j, 1)
arr(j, 1) = s
End If
Next
Next
For i = 1 To UBound(arr)
For j = i + 1 To UBound(arr)
si = Split(arr(i, 1), "-")
sj = Split(arr(j, 1), "-")
If si(0) = sj(0) Then
If si(1) + 1 > sj(1) + 1 Then '+1是讓"文字型的數字"變為"數字"
s = arr(i, 1)
arr(i, 1) = arr(j, 1)
arr(j, 1) = s
End If
End If
Next
Next
[a4].Resize(UBound(arr)) = arr
End Sub
複製代碼
作者:
GBKEE
時間:
2015-7-1 15:15
回復
1#
ui123
是這樣嗎?
Option Explicit
Sub Ex()
Dim Rng As Range, Sp As Variant
Set Rng = Range("a4")
Do While Rng <> ""
Sp = Rng
For i = 1 To Len(Rng)
If Mid(Rng, i, 1) Like "[!A-z]" And Mid(Rng, i, 1) Like "[!0-9]" Then
'Mid(Rng, i, 1) 不是文字,數字.
Sp = Replace(Sp, Mid(Sp, i, 1), ",") '+"-"或"/"或"#" 替換為 ","
End If
Next
Sp = Split(Sp, ",")
With Rng.Cells(1, 2).Resize(1, UBound(Sp) + 1)
.Value = Sp
.Value = .Value
End With
Set Rng = Rng.Offset(1)
Loop
End Sub
複製代碼
作者:
ui123
時間:
2015-7-2 17:39
回復
4#
GBKEE
我明天去公司試試看 ^^~ 先謝謝囉!
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)