Option Explicit
' Downloaded from www.contextures.com
'*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
'* NumsToWords(NumSource, MajorCurrency, MinorCurrency, MajorMinorLink) function *
'* *
'* Where:Words *
'* NumSource: Number, or cell reference containing the number, to be converted to words *
'* MajorCurrency: Primary currency name.......................... (Optional: Default is "Dollar") *
'* MinorCurrency: Secondary currency name........................ (Optional: Default is "Cent") *
'* MajorMinorLink: Word to connect Major and Minor Currency....... (Optional: Default is "and") *
'* SkipMinor: True/False flag to ignore the MinorCurrency.... (Optional: Default is FALSE) *
'* *
'* Programmer: Ron Coderre *
'* Created on: 14-JUL-2007 *
'* Last Modified: 24-MAR-2009 *
'*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
Public Function NumsToWords( _
NumSource As Currency, _
Optional MajorCurrency As String = "Dollar", _
Optional MinorCurrency As String = "Cent", _
Optional MajorMinorLink As String = "and", _
Optional SkipMinor As Boolean = False _
) As String
Dim Words As String ' Used to build the word phrase
Dim WIPnum As String ' Orig number formatted as 000000000000000.00
Dim LU_NumList() ' Array of numbers to match during the process
Dim LU_NumText() ' Text values associated with LU_NumList values
Dim iMisc As Integer ' Container for interim calculations
Dim iCtr As Integer ' Counter variable
Dim LU_Denom() ' Array of groups (Trillion, Billion, etc)
Dim DecSepChar ' Decimal separator symbol ( eg English: . )
LU_NumList = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, _
11, 12, 13, 14, 15, 16, 17, 18, 19, _
20, 30, 40, 50, 60, 70, 80, 90)
LU_NumText = Array("", " One", " Two", " Three", " Four", " Five", _
" Six", " Seven", " Eight", " Nine", " Ten", " Eleven", _
" Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", _
" Seventeen", " Eighteen", " Nineteen", " Twenty", " Thirty", _
" Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety")
DecSepChar = Application.International(xlDecimalSeparator)
LU_Denom = Array(" Trillion", " Billion", " Million", " Thousand", "", "")
WIPnum = Replace(Format(Abs(NumSource), "000000000000000.00;KillFlow"), DecSepChar, "0")
'Pull successive WIPnum triads and assign word values
For iCtr = 0 To 5
iMisc = CInt(Mid(WIPnum, (1 + iCtr * 3), 3))
If Int(iMisc / 100) > 0 Then Words = Words & LU_NumText(Int(iMisc / 100)) & " Hundred"
'Set the tens and ones phrase
If (iMisc Mod 100) > 19 Then
Words = Words & LU_NumText(Int((iMisc Mod 100) / 10) + 18) & LU_NumText(iMisc Mod 10)
Else
Words = Words & LU_NumText(iMisc Mod 100)
End If
If iMisc > 0 Then Words = Words & LU_Denom(iCtr)
If iCtr = 4 Then ' Finish building the whole nums phrase
Words = Words & " " & MajorCurrency
If Int(NumSource) = 0 Then Words = "No" & Words
If Int(NumSource) <> 1 And MajorCurrency <> "" Then Words = Words & "s"
If SkipMinor = False Then Words = Words & " " & MajorMinorLink Else Exit For
ElseIf iCtr = 5 Then 'Complete the MinorCurrency phrase
If SkipMinor = False Then
If iMisc = 0 Then Words = Words & " No"
Words = Words & " " & MinorCurrency
If iMisc <> 1 And MinorCurrency <> "" Then Words = Words & "s"
End If
End If
Next iCtr
NumsToWords = Trim(Replace(Words, " ", " "))
End Function
Bookmarks