上網找到的方法都有固定格式,所以用mid、left、right就能取出來轉換
但我遇到的問題如下:
如下舉例:
14d → 十四D
A223→ A二二三
1A17B→ 一A十七B
字元數不超過10個
目前我是列出10個儲存格,分別存放每個字元轉成國字或雙字元,再合併
不知道能不能用函數在一個儲存格就完成
2016/12/28補充
感謝各位的回答,關於17轉十七的問題,補充說明一下
取出的數字超過99(三位數以上),則不顯示「十」「百」「千」等單位
如:
27 → 二十七
1348 → 一三四八
2016/12/28回應,(抱歉還沒過新手任務,請容許小弟用編輯的方式回應)
感謝wonton的解答,而且有很多需要用到數字轉國字的地方都能用此巨集稍做微調完成
原本的code有一個小bug,就是40、50…等會變成四十零、五十零,經過調整已修正
再次感謝wonton的解答
第一步,打開開發人員標籤
https://support.office.com/zh-hk/article/-e1192344-5e56-4d45-931b-e5fd9bea2d45
第二步,插入自訂公式,範例如下
http://blog.twtnn.com/2014/09/excel.html
我寫了一個 Num2Str 自訂公式,代碼如下
Function Num2Str(Str As String) As String
For i = 1 To Len(Str)
a = Mid(Str, i, 1)
Select Case True
Case a = 0
aa = aa & "零"
Case a = 1
aa = aa & "一"
Case a = 2
aa = aa & "二"
Case a = 3
aa = aa & "三"
Case a = 4
aa = aa & "四"
Case a = 5
aa = aa & "五"
Case a = 6
aa = aa & "六"
Case a = 7
aa = aa & "七"
Case a = 8
aa = aa & "八"
Case a = 9
aa = aa & "九"
Case Else
aa = aa & a
End Select
Next i
Num2Str = aa
End Function
執行結果如下:
剛剛重看了一次你的題目,你要將 17 換成「十七」,而非「一七」,那就還要再微調一下 Function 內容...
正解!
基本上17要轉為十七應該是樓主的"筆誤"或是未理解.
如果17要轉為十七, 那27是要轉為二十七還是二七?
同理: 那127,1234,12345呢?
正解無誤!
17是一七還是十七?
往上一看
223是二二三而不是兩百二十三
樓主的問題
沒有一致性可遵循
題目有補充說明
看來
只有您的答案比較適合(修改成)正解了
我用了一個土法煉鋼的方式,先將字串分析,將數字和英文字母拆開存到 Array 中,然後再依據雙位數跟非雙位數處理,全部的函數如下:
Function StrParse(Str As String) As String
Dim a_str(10)
Index = 1
For i = 1 To Len(Str)
a = Mid(Str, i, 1)
If IsNumeric(a) Then
a_str(Index) = a_str(Index) & a
For j = i + 1 To Len(Str)
b = Mid(Str, j, 1)
If IsNumeric(b) Then
a_str(Index) = a_str(Index) & b
i = j
Else
i = j - 1
j = Len(Str)
Index = Index + 1
End If
Next j
Else
a_str(Index) = a_str(Index) & a
For j = i + 1 To Len(Str)
b = Mid(Str, j, 1)
If IsNumeric(b) Then
i = j - 1
j = Len(Str)
Index = Index + 1
Else
a_str(Index) = a_str(Index) & b
i = j
End If
Next j
End If
Next i
For i = 1 To 10
If IsNumeric(a_str(i)) And Len(a_str(i)) = 2 Then
aa = aa & DoubleDigits2Str(CStr(a_str(i)))
Else
aa = aa & Num2Str(CStr(a_str(i)))
End If
Next i
StrParse = aa
End Function
Function DoubleDigits2Str(Str As String) As String
a = Mid(Str, 1, 1)
Select Case True
Case a = 0
aa = Num2Str(Mid(Str, 1, 1)) & Num2Str(Mid(Str, 2, 1))
Case a = 1
aa = "十" & Num2Str(Mid(Str, 2, 1))
Case Else
aa = Num2Str(Mid(Str, 1, 1)) & "十" & Num2Str(Mid(Str, 2, 1))
End Select
DoubleDigits2Str = aa
End Function
Function Num2Str(Str As String) As String
For i = 1 To Len(Str)
a = Mid(Str, i, 1)
Select Case True
Case a = 0
aa = aa & "零"
Case a = 1
aa = aa & "一"
Case a = 2
aa = aa & "二"
Case a = 3
aa = aa & "三"
Case a = 4
aa = aa & "四"
Case a = 5
aa = aa & "五"
Case a = 6
aa = aa & "六"
Case a = 7
aa = aa & "七"
Case a = 8
aa = aa & "八"
Case a = 9
aa = aa & "九"
Case Else
aa = aa & a
End Select
Next i
Num2Str = aa
End Function
然後執行結果如下:
好久沒寫 VBA 了,語法忘光光...
另一個比較low的做法:substitute
//處理 A1
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"0","零"),"1","一"),"2","二"),"3","三"),"4","四"),"5","五"),"6","六"),"7","七"),"8","八"),"9","九")