相信很多人都會需要將數字金額用文字方式呈現,尤其在建立發票資料等重要文件時,筆者因工作關係,同事的需求,上網尋找解決方案,因此有了現成的解決方案,但發現了解決方案有錯誤的情況發生,且同是有特定的輸出需求,因此又改了一下內容,在此與大家分享。原程式由Chris Mead撰寫,這個版本我已另外改寫,有需要原始版峎的可參考原始網頁。
建立一個模組,將以下程式貼到該模組使用:
Option Compare Database
Public Numbers As Variant, Tens As Variant
Sub SetNums()
Numbers = Array("", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN")
Tens = Array("", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY")
End Sub
Function WordNum(MyNumber As Double) As String
Dim DecimalPosition As Integer, ValNo As Variant, StrNo As String
Dim NumStr As String, n As Integer, Temp1 As String, Temp2 As String
Dim Temp1_1 As String, Temp2_1 As String, WordNumDec As String
Dim NumStr2 As String
' This macro was written by Chris Mead - www.MeadInKent.co.uk
' http://www.meadinkent.co.uk/xlnumberstext.htm
' 2013/5/8 Andy Chiu 修改為符合公司商業使用語法
' 2013/10/1 Andy Chiu 修改判斷DOLLAR與CENT是否為單數,顯示單數顯示方式
If Abs(MyNumber) > 999999999 Then
WordNum = "Value too large"
Exit Function
End If
SetNums
' Values after the decimal place
NumStr = Format(Trim(Str(Abs(MyNumber))), "#.00") 'fixed to .00
DecimalPosition = InStr(NumStr, ".")
'Numbers(0) = "Zero"
If DecimalPosition > 0 Then
NumStr2 = Mid(NumStr, DecimalPosition + 1, Len(NumStr) - DecimalPosition)
Else
NumStr2 = 0
End If
If DecimalPosition > 0 And DecimalPosition < Len(NumStr) And CLng(NumStr2) > 0 Then
Temp1 = " AND "
NumStr2 = Mid(NumStr, DecimalPosition + 1, Len(NumStr) - DecimalPosition)
NumStr2 = Right("000000000" & Trim(NumStr2), 9)
ValNo = Array(0, Val(Mid(NumStr2, 1, 3)), Val(Mid(NumStr2, 4, 3)), Val(Mid(NumStr2, 7, 3)))
For n = 3 To 1 Step -1 'analyse the absolute number as 3 sets of 3 digits
StrNo = Format(ValNo(n), "000")
If ValNo(n) > 0 Then
Temp1_1 = GetTens(Val(Right(StrNo, 2)))
If Left(StrNo, 1) <> "0" Then
Temp2_1 = Numbers(Val(Left(StrNo, 1))) & " HUNDRED "
If Temp1_1 <> "" Then Temp2_1 = Temp2_1 & " AND "
Else
Temp2_1 = ""
End If
If n = 3 Then
If Temp2_1 = "" And ValNo(1) + ValNo(2) > 0 Then Temp2_1 = "AND "
WordNumDec = Trim(Temp2_1 & Temp1_1)
End If
If n = 2 Then WordNumDec = Trim(Temp2_1 & Temp1_1 & " THOUSAND " & WordNumDec)
If n = 1 Then WordNumDec = Trim(Temp2_1 & Temp1_1 & " MILLION " & WordNumDec)
End If
Next n
End If
' String representation of amount (excl decimals)
NumStr = Right("000000000" & Trim(Str(Int(Abs(MyNumber)))), 9)
ValNo = Array(0, Val(Mid(NumStr, 1, 3)), Val(Mid(NumStr, 4, 3)), Val(Mid(NumStr, 7, 3)))
For n = 3 To 1 Step -1 'analyse the absolute number as 3 sets of 3 digits
StrNo = Format(ValNo(n), "000")
If ValNo(n) > 0 Then
Temp1 = GetTens(Val(Right(StrNo, 2)))
If Left(StrNo, 1) <> "0" Then
Temp2 = Numbers(Val(Left(StrNo, 1))) & " HUNDRED "
If Temp1 <> "" And WordNumDec = "" Then Temp2 = Temp2 & " AND "
Else
Temp2 = ""
End If
If n = 3 Then
If Temp2 = "" And ValNo(1) + ValNo(2) > 0 And WordNumDec = "" Then Temp2 = "AND "
WordNum = Trim(Temp2 & Temp1)
End If
If n = 2 Then WordNum = Trim(Temp2 & Temp1 & " THOUSAND " & WordNum)
If n = 1 Then WordNum = Trim(Temp2 & Temp1 & " MILLION " & WordNum)
End If
Next n
If WordNumDec <> "" And WordNum = "ONE" And WordNumDec = "ONE" Then
WordNum = WordNum & " DOLLAR AND " & WordNumDec & " CENT ONLY."
ElseIf WordNumDec <> "" And WordNum = "ONE" Then
WordNum = WordNum & " DOLLAR AND " & WordNumDec & " CENTS ONLY."
ElseIf WordNumDec <> "" And WordNumDec = "ONE" Then
WordNum = WordNum & " DOLLARS AND " & WordNumDec & " CENT ONLY."
ElseIf WordNumDec <> "" Then
WordNum = WordNum & " DOLLARS AND " & WordNumDec & " CENTS ONLY."
ElseIf WordNum = "ONE" Then
WordNum = WordNum & " DOLLAR ONLY."
Else
WordNum = WordNum & " DOLLARS ONLY."
End If
If Len(WordNum) = 0 Or Left(WordNum, 2) = " p" Or MyNumber = 0 Then
WordNum = "ZERO" & WordNum
End If
End Function
Function GetTens(TensNum As Integer) As String
' Converts a number from 0 to 99 into text.
If TensNum <= 19 Then
GetTens = Numbers(TensNum)
Else
Dim MyNo As String
MyNo = Format(TensNum, "00")
GetTens = Tens(Val(Left(MyNo, 1))) & " " & Numbers(Val(Right(MyNo, 1)))
End If
End Function
Sub WordNum測試()
Debug.Print WordNum(12345.99) ‘DOLLAR與CENT都複數
Debug.Print WordNum(1.99) ‘DOLLAR單數
Debug.Print WordNum(99.01) ‘CENT單數
End Sub
「WordNum測試()」執行後的結果:
TWELVE THOUSAND THREE HUNDRED FORTY FIVE DOLLARS AND NINETY NINE CENTS ONLY.
ONE DOLLAR AND NINETY NINE CENTS ONLY.
NINETY NINE DOLLARS AND ONE CENT ONLY.