Categories

Users Online

+ 25 Guests + 6 Bots

How to Convert Currency Numbers to English Text String in MS Excel 2007 and Excel 2010

CURRENCY© Ugur Akinci

MS Excel 2007 and Excel 2010 come with a numbers-to-text conversion function called BAHTTEXT().

Yet there is a surprise waiting for you there: the text string displays in Thai and there’s no easy way to translate that into English:

MS Excel 2007 and 2010 - BAHTTEXT Function

But what if you wanted to express the number in English and what if you wanted the text to display a currency unit other than dollars and cents?

You can accomplish that by creating a =SPELLNUMBER() function macro.

Follow these steps:

(1) Open your MS Excel document
(2) Press Alt + F11 to launch your Visual Basic Editor.
(3) Select Insert > Module from the menu bar to display the module code window:

MS Excel 2007 Excel 2010 Convert Numbers to Text

(4) Copy and paste the following code into the window (code courtesy of Microsoft):
——————————————————————
Option Explicit
‘Main Function
Function SpellNumber(ByVal MyNumber)
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.
MyNumber = Trim(Str(MyNumber))
‘ Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, “.”)
‘ Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
“00″, 2))
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 = “No Dollars”
Case “One”
Dollars = “One Dollar”
Case Else
Dollars = Dollars & ” Dollars”
End Select
Select Case Cents
Case “”
Cents = ” and No Cents”
Case “One”
Cents = ” and One Cent”
Case Else
Cents = ” and ” & Cents & ” Cents”
End Select
SpellNumber = Dollars & Cents
End Function

‘ Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
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)
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)
Select Case Val(Digit)
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”
Case Else: GetDigit = “”
End Select
End Function
———————————————–

(5) Save the code file in the “.xlsm” format to preserve the macro you’ve just created.
(6) Click Alt + F11 again to go back to the Excel sheet.
(7) In the formula bar, type the following for a test:

=SpellNumber(44.23)

and the result will be displayed in text as:

Forty Four Dollars and Twenty Three Cents

MS Excel 2007 Excel 2010 Convert Numbers to Text SPELLNUMBER MACRO

(8) To change the currency labels, in the macro editing window, do a Find-and-Replace.

For example, replace all “Dollar” text with “Ruble” and all “Cent” text with “Kopek“.

Save your macro.

Run the macro again in Excel sheet, as described on step 7.

This time the result will be:

MS Excel 2007 Excel 2010 Convert Numbers to Text SPELLNUMBER MACRO 2


Share

4 comments to How to Convert Currency Numbers to English Text String in MS Excel 2007 and Excel 2010

  • Elizabeth Suarez

    set up a macrom, but a compile error: Expected: expression comes up when I assign the formula. What is the problem or the solution to resolv e this issue?

  • Elizabeth, thanks for reading TCC. Excel is expecting a certain value in the formula but it’s not finding it. Please re-check all the components of the formula and make sure they’re all there. Regards, Ugur

  • Hi,
    I’m sorry but it seems the spelling trouble is in this article. I got the same problem and found that at the second line there is a quotation mark instead of an apostrophe. There may be more but you should just copy the whole thing from Microsoft’s knowledge base article number 213360 (http://support.microsoft.com/kb/213360). I have seen this happen before; I believe it occurs when trying to copy/paste between formatted text and code.

  • Flint

    Dear Sir / Madame,

    I do need your assistance regarding the code. I followed the step to create a module to run the SpellNumber, but an error occured, I don’t know why, I just copy and paste, but it could not work. Kindly advice. Thanks much.

Leave a Reply

  

  

  


*

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>