Answered by:
SPELL NUMBERS INCLUDING DECIMAL

Question
-
Hi,
I want to spell the numbers like,
566716.00 as "Five Hundred Eighty Thousand Eight Hundred Fifty Point Zero Zero"
566716.10 as "Five Hundred Eighty Thousand Eight Hundred Fifty Point One Zero"
566716.01 as "Five Hundred Eighty Thousand Eight Hundred Fifty Point Zero One"
566716.012 as "Five Hundred Eighty Thousand Eight Hundred Fifty Point Zero One Two"
566716.0123 as "Five Hundred Eighty Thousand Eight Hundred Fifty Point Zero One Two Three"
Could you please help me to fix this problem?
Saturday, February 15, 2014 9:35 AM
Answers
-
Here is code you can use, adapted from Microsoft's example of spelling numbers.
Function SpellNumber(ByVal MyNumber As Variant) As String Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Thousand " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion " ' String representation of amount. If TypeName(MyNumber) = "Range" Then MyNumber = Trim(MyNumber.Text) Else MyNumber = Trim(CStr(MyNumber)) End If ' Position of decimal place 0 if none. DecimalPlace = InStr(MyNumber, ".") ' Convert cents and set MyNumber to dollar amount. If DecimalPlace > 0 Then Cents = " Point " & SpellDigits(Mid(MyNumber, DecimalPlace + 1)) MyNumber = Trim(Left(MyNumber, DecimalPlace - 1)) End If Count = 1 Do While MyNumber <> "" Temp = GetHundreds(Right(MyNumber, 3)) If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars If Len(MyNumber) > 3 Then MyNumber = Left(MyNumber, Len(MyNumber) - 3) Else MyNumber = "" End If Count = Count + 1 Loop Select Case Dollars Case "" Dollars = "Zero" End Select SpellNumber = Dollars & Cents End Function ' Converts a number from 100-999 into text Function GetHundreds(ByVal MyNumber As String) As String Dim Result As String If Val(MyNumber) = 0 Then Exit Function MyNumber = Right("000" & MyNumber, 3) ' Convert the hundreds place. If Mid(MyNumber, 1, 1) <> "0" Then Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred " End If ' Convert the tens and ones place. If Mid(MyNumber, 2, 1) <> "0" Then Result = Result & GetTens(Mid(MyNumber, 2)) Else Result = Result & GetDigit(Mid(MyNumber, 3)) End If GetHundreds = Result End Function ' Converts a number from 10 to 99 into text. Function GetTens(TensText As String) As String Dim Result As String Result = "" ' Null out the temporary function value. If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19... Select Case Val(TensText) Case 10: Result = "Ten" Case 11: Result = "Eleven" Case 12: Result = "Twelve" Case 13: Result = "Thirteen" Case 14: Result = "Fourteen" Case 15: Result = "Fifteen" Case 16: Result = "Sixteen" Case 17: Result = "Seventeen" Case 18: Result = "Eighteen" Case 19: Result = "Nineteen" Case Else End Select Else ' If value between 20-99... Select Case Val(Left(TensText, 1)) Case 2: Result = "Twenty " Case 3: Result = "Thirty " Case 4: Result = "Forty " Case 5: Result = "Fifty " Case 6: Result = "Sixty " Case 7: Result = "Seventy " Case 8: Result = "Eighty " Case 9: Result = "Ninety " Case Else End Select Result = Result & GetDigit _ (Right(TensText, 1)) ' Retrieve ones place. End If GetTens = Result End Function ' Converts a number from 1 to 9 into text. Function GetDigit(Digit As String, Optional ShowZero As Boolean) As String Select Case Val(Digit) Case 0 If ShowZero Then GetDigit = "Zero" End If Case 1 GetDigit = "One" Case 2 GetDigit = "Two" Case 3 GetDigit = "Three" Case 4 GetDigit = "Four" Case 5 GetDigit = "Five" Case 6 GetDigit = "Six" Case 7 GetDigit = "Seven" Case 8 GetDigit = "Eight" Case 9 GetDigit = "Nine" End Select End Function ' Spells digits Function SpellDigits(s As String) As String Dim i As Long Dim Result As String For i = 1 To Len(s) Result = Result & " " & GetDigit(Mid(s, i, 1), True) Next i SpellDigits = Trim(Result) End Function
Use like this:
=SpellNumber(12.34)
or
=SpellNumber(B37)
where B37 is a cell containing a number.
Regards, Hans Vogelaar (http://www.eileenslounge.com)
Saturday, February 15, 2014 11:42 AM -
Are you using a thousands separator? The code doesn't take that into account.
The following version does. I've only posted the first part of the function, the rest remains unchanged:
Function SpellNumber(ByVal MyNumber As Variant) As String Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Thousand " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion " ' String representation of amount. If TypeName(MyNumber) = "Range" Then MyNumber = Replace(Trim(MyNumber.Text), _ Application.International(xlThousandsSeparator), "") Else MyNumber = Replace(Trim(CStr(MyNumber)), _ Application.International(xlThousandsSeparator), "") End If ' Position of decimal place 0 if none. DecimalPlace = InStr(MyNumber, Application.International(xlDecimalSeparator)) ' ...
Regards, Hans Vogelaar (http://www.eileenslounge.com)
- Marked as answer by George Hua Friday, March 7, 2014 2:23 AM
Friday, February 28, 2014 5:14 PM
All replies
-
Here is code you can use, adapted from Microsoft's example of spelling numbers.
Function SpellNumber(ByVal MyNumber As Variant) As String Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Thousand " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion " ' String representation of amount. If TypeName(MyNumber) = "Range" Then MyNumber = Trim(MyNumber.Text) Else MyNumber = Trim(CStr(MyNumber)) End If ' Position of decimal place 0 if none. DecimalPlace = InStr(MyNumber, ".") ' Convert cents and set MyNumber to dollar amount. If DecimalPlace > 0 Then Cents = " Point " & SpellDigits(Mid(MyNumber, DecimalPlace + 1)) MyNumber = Trim(Left(MyNumber, DecimalPlace - 1)) End If Count = 1 Do While MyNumber <> "" Temp = GetHundreds(Right(MyNumber, 3)) If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars If Len(MyNumber) > 3 Then MyNumber = Left(MyNumber, Len(MyNumber) - 3) Else MyNumber = "" End If Count = Count + 1 Loop Select Case Dollars Case "" Dollars = "Zero" End Select SpellNumber = Dollars & Cents End Function ' Converts a number from 100-999 into text Function GetHundreds(ByVal MyNumber As String) As String Dim Result As String If Val(MyNumber) = 0 Then Exit Function MyNumber = Right("000" & MyNumber, 3) ' Convert the hundreds place. If Mid(MyNumber, 1, 1) <> "0" Then Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred " End If ' Convert the tens and ones place. If Mid(MyNumber, 2, 1) <> "0" Then Result = Result & GetTens(Mid(MyNumber, 2)) Else Result = Result & GetDigit(Mid(MyNumber, 3)) End If GetHundreds = Result End Function ' Converts a number from 10 to 99 into text. Function GetTens(TensText As String) As String Dim Result As String Result = "" ' Null out the temporary function value. If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19... Select Case Val(TensText) Case 10: Result = "Ten" Case 11: Result = "Eleven" Case 12: Result = "Twelve" Case 13: Result = "Thirteen" Case 14: Result = "Fourteen" Case 15: Result = "Fifteen" Case 16: Result = "Sixteen" Case 17: Result = "Seventeen" Case 18: Result = "Eighteen" Case 19: Result = "Nineteen" Case Else End Select Else ' If value between 20-99... Select Case Val(Left(TensText, 1)) Case 2: Result = "Twenty " Case 3: Result = "Thirty " Case 4: Result = "Forty " Case 5: Result = "Fifty " Case 6: Result = "Sixty " Case 7: Result = "Seventy " Case 8: Result = "Eighty " Case 9: Result = "Ninety " Case Else End Select Result = Result & GetDigit _ (Right(TensText, 1)) ' Retrieve ones place. End If GetTens = Result End Function ' Converts a number from 1 to 9 into text. Function GetDigit(Digit As String, Optional ShowZero As Boolean) As String Select Case Val(Digit) Case 0 If ShowZero Then GetDigit = "Zero" End If Case 1 GetDigit = "One" Case 2 GetDigit = "Two" Case 3 GetDigit = "Three" Case 4 GetDigit = "Four" Case 5 GetDigit = "Five" Case 6 GetDigit = "Six" Case 7 GetDigit = "Seven" Case 8 GetDigit = "Eight" Case 9 GetDigit = "Nine" End Select End Function ' Spells digits Function SpellDigits(s As String) As String Dim i As Long Dim Result As String For i = 1 To Len(s) Result = Result & " " & GetDigit(Mid(s, i, 1), True) Next i SpellDigits = Trim(Result) End Function
Use like this:
=SpellNumber(12.34)
or
=SpellNumber(B37)
where B37 is a cell containing a number.
Regards, Hans Vogelaar (http://www.eileenslounge.com)
Saturday, February 15, 2014 11:42 AM -
Thank you so much..
Gokul
Monday, February 17, 2014 4:54 AM -
Is there any possibility to do the above said formula for wise versa scenario? That is,
"Eight Point Zero" as 8.0
"Eight Point Zero One" as 8.01
"One Hundred Twenty Three Point One" as 123.1
"One Hundred Twenty Three Point One Zero" as 123.10
"Six Hundred Fifty Four Thousand Three Hundred Twenty Three Point One Two" as 654321.12
Could you help on this?
Gokul
Monday, February 17, 2014 5:53 AM -
See the replies in http://social.msdn.microsoft.com/Forums/en-US/a0721eda-1995-47d4-99c0-8aa5f05858ea/read-numbers-from-words?forum=exceldev
Regards, Hans Vogelaar (http://www.eileenslounge.com)
Tuesday, February 18, 2014 3:32 PM -
Hi,
The Function which you have sent earlier shows the error for the following cases and its similars..
658123.01 - Five Hundred Thousand One Hundred Thirty Three Point Zero One125623.1 - Two Hundred Thousand Six Hundred Thirty Three Point One Zero1125623.1 - One Hundred Million Two Hundred Thousand Six Hundred Thirty Three Point One Zero
Pls help on this issue..
Gokul.
Gokul
Friday, February 28, 2014 11:27 AM -
It works correctly for me. Are you sure that you copied all the code correctly?
Regards, Hans Vogelaar (http://www.eileenslounge.com)
Friday, February 28, 2014 3:08 PM -
Are you using a thousands separator? The code doesn't take that into account.
The following version does. I've only posted the first part of the function, the rest remains unchanged:
Function SpellNumber(ByVal MyNumber As Variant) As String Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Thousand " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion " ' String representation of amount. If TypeName(MyNumber) = "Range" Then MyNumber = Replace(Trim(MyNumber.Text), _ Application.International(xlThousandsSeparator), "") Else MyNumber = Replace(Trim(CStr(MyNumber)), _ Application.International(xlThousandsSeparator), "") End If ' Position of decimal place 0 if none. DecimalPlace = InStr(MyNumber, Application.International(xlDecimalSeparator)) ' ...
Regards, Hans Vogelaar (http://www.eileenslounge.com)
- Marked as answer by George Hua Friday, March 7, 2014 2:23 AM
Friday, February 28, 2014 5:14 PM