返回列表 上一主題 發帖

[分享] 金額數字轉換英文寫法(VBA自訂函數)

[分享] 金額數字轉換英文寫法(VBA自訂函數)

金額數字轉換英文寫法.VBA自訂函數  

Function USNumber(ByVal MyNumber, QType) As String
Dim i%, j%, TR, StrCT$, StrDR$, TT$, TU$, Num
If Val(MyNumber) <= 0 Then Exit Function
TR = Array("", "", " Thousand", " Million", " Billion", " Trillion")
'-小數部份---------------------------
Num = Split(Format(MyNumber, "0.00"), ".")
TT = Get999(Num(1))
If TT <> "" Then StrCT = "Cents " & TT
'-整數部份---------------------------
Num = Split(Format(Num(0), "#,##0"), ",")
For i = UBound(Num) To 0 Step -1
    TT = Get999(Num(i))
    j = j + 1:  TU = Trim(TT & TR(j) & " " & TU)
Next i
StrDR = Trim(TU & " " & IIf(QType = 1, "Dollars", ""))
'---------------------------------
USNumber = StrDR & IIf(StrDR = "" Or StrCT = "", "", " And ") & StrCT & " Only"
End Function

'=====================================
Function Get999(TTNum) As String
Dim TN, TY, QQ%, GStr1$, GStr2$
TN = Split("-One-Two-Three-Four-Five-Six-Seven-Eight-Nine-Ten-Eleven-Twelve-Thirteen-Fourteen-Fifteen-Sixteen-Seventeen-Eighteen-Nineteen", "-")
TY = Split("--Twenty-Thirty-Forty-Fifty-Sixty-Seventy-Eighty-Ninety", "-")
'-百位數---------------------------
GStr1 = TN(Int(TTNum / 100)) & " Hundred"
If GStr1 = " Hundred" Then GStr1 = ""
'-十/個位數---------------------------
QQ = TTNum Mod 100
GStr2 = Replace(Trim(TY(Int(Right(QQ, 2) / 10)) & " " & TN(QQ Mod 10)), " ", "-")
If QQ < 20 Then GStr2 = TN(QQ)
Get999 = Trim(GStr1 & " " & GStr2)
End Function

'=================================
參考檔:
20200321a01(金額數字英文寫法).rar (11.23 KB)

程式碼不難, 若有不同格式需求, 請自行修改~~
EXCEL參考資料:
http://blog.xuite.net/smile1000mile/blog

        靜思自在 : 做該做的事是智慧,做不該做的事是愚癡。
返回列表 上一主題