Wednesday, September 23, 2020

Spell USD Amount into Words

In the Microsoft Excel there is no default function that displays the numbers into English words in a worksheet, but you can add this capability by creating your own function by pasting the following SpellNumberToEnglish function code into a VBA (Visual Basic for Applications) module. 

This function lets you convert the dollar and cent amounts to words with a formula, so $549.99 would read as Five Hundred Forty Nine Dollars and Ninety Nine Cents. This can be very useful if you're using Excel as a template to print Cheques/Checks. 

 


To add this function into your workbook, please follow the beneath steps:

  1. Open the workbook where you want to spell the numbers.
  2. Press Alt+F11 key from your keyboard to open the Visual Basic Editor (VBE) window.
  3. Click the Insert tab in the VBE, and click on the Module option

    VBE - Insert Module Image
    VBE - Insert Module

  4. You will see a window named YourWorkBook Name - Module1 as shown in the below screenshot:

    Code window in Module Image
    Code window in Module



  5. Copy and Paste the following lines of codes in this module:
  6. Function SpellNumberToEnglish (ByVal pNumber)
    Dim arr, xDecimal, xIndex, xHundred, xValue As Variant
    Dim Dollars, Cents
    arr = Array("", "", " Thousand ", " Million ", " Billion ", " Trillion ")
    pNumber = Trim(Str(pNumber))
    xDecimal = InStr(pNumber, ".")
    If xDecimal > 0 Then
    Cents = GetTens(Left(Mid(pNumber, xDecimal + 1) & "00", 2))
    pNumber = Trim(Left(pNumber, xDecimal - 1))
    End If
    xIndex = 1
    Do While pNumber <> ""
    xHundred = ""
    xValue = Right(pNumber, 3)
    If Val(xValue) <> 0 Then
    xValue = Right("000" & xValue, 3)
    If Mid(xValue, 1, 1) <> "0" Then
    xHundred = GetDigit(Mid(xValue, 1, 1)) & " Hundred "
    End If
    If Mid(xValue, 2, 1) <> "0" Then
    xHundred = xHundred & GetTens(Mid(xValue, 2))
    Else
    xHundred = xHundred & GetDigit(Mid(xValue, 3))
    End If
    End If
    If xHundred <> "" Then
    Dollars = xHundred & arr(xIndex) & Dollars
    End If
    If Len(pNumber) > 3 Then
    pNumber = Left(pNumber, Len(pNumber) - 3)
    Else
    pNumber = ""
    End If
    xIndex = xIndex + 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
    SpellNumberToEnglish = Dollars & Cents
    End Function
    Private Function GetTens(pTens)
    Dim Result As String
    Result = ""
    If Val(Left(pTens, 1)) = 1 Then
    Select Case Val(pTens)
    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
    Select Case Val(Left(pTens, 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(pTens, 1))
    End If
    GetTens = Result
    End Function
    Private Function GetDigit(pDigit)
    Select Case Val(pDigit)
    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
  7. Press Ctrl+S to save the updated workbook.

    Note: When you try to save the workbook with a macro you'll get the message "The following features cannot be saved in macro-free workbook"

    Cannot Save VBproject Image
    Cannot Save VB Project



    Click No. When you see a new dialog, chose the Save As option. In the field "Save as type" pick the option "Excel macro-enabled workbook".

    SaveAs Macro Enabled Excel Workbook Image
    SaveAs Macro Enabled Excel Workbook

    Congratulations! Now, you have added the SpellNumberToEnglish UDF Function to your workbook successfully.

Use SpellNumberToEnglish function in your worksheets


Now you can use the function SpellNumberToEnglish in your Excel Workbook. Enter =SpellNumberToEnglish(A2) into the cell where you need to get the number written in words. Here A2 is the address of the cell with the number or amount. 
 
Spell Number To English Function Example GIF Image
Spell Number To English Function Example

Other Related UDF Functions:


UDF to Spell INR Amount into Words

6 comments: