0% found this document useful (0 votes)
19 views3 pages

E10.5 Spell Formula Code

The document contains a VBA function called SpellNumber that converts a numeric value into its English words representation, including currency formatting. It handles both whole numbers and decimal values, with additional support for specifying currency names and sub-currencies. The function utilizes a helper function, ConvertHundreds, to break down the number into hundreds, tens, and ones for conversion.

Uploaded by

meghnazadge01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

E10.5 Spell Formula Code

The document contains a VBA function called SpellNumber that converts a numeric value into its English words representation, including currency formatting. It handles both whole numbers and decimal values, with additional support for specifying currency names and sub-currencies. The function utilizes a helper function, ConvertHundreds, to break down the number into hundreds, tens, and ones for conversion.

Uploaded by

meghnazadge01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function SpellNumber(ByVal MyNumber As Variant, Optional ByVal CurrencyName As

String = "Dollars", Optional ByVal SubCurrency As String = "Cents") As String


On Error GoTo ErrorHandler
Dim Units As String, Tens As String, Hundreds As String
Dim DecimalPlace As Integer, Count As Integer
Dim NumberText As String
Dim Place(9) As String
Dim DecimalNumber As String
Dim TempStr As String
Dim DecimalStr As String

' Check if the input is numeric


If Not IsNumeric(MyNumber) Then
SpellNumber = "#VALUE!"
Exit Function
End If

Place(2) = " Thousand "


Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "

' Convert MyNumber to a string and trim extra spaces


MyNumber = Trim(CStr(MyNumber))

' Find position of decimal point (if any) and convert to integer
DecimalPlace = InStr(MyNumber, ".")

' Convert decimal portion of number (if any) to text


If DecimalPlace > 0 Then
DecimalStr = Mid(MyNumber, DecimalPlace + 1)
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
Else
DecimalStr = ""
End If

Count = 1
Do While MyNumber <> ""
DecimalNumber = ""
Hundreds = Left(MyNumber, 3)

If Len(MyNumber) > 3 Then


MyNumber = Right(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If

If Hundreds <> "" Then


DecimalNumber = ConvertHundreds(Hundreds)
NumberText = DecimalNumber & Place(Count) & NumberText
End If

Count = Count + 1
Loop

SpellNumber = NumberText & CurrencyName & " and " & ConvertHundreds(DecimalStr)
& " " & SubCurrency
Exit Function
ErrorHandler:
SpellNumber = "#VALUE!"
End Function

Function ConvertHundreds(ByVal MyNumber)


Dim Result As String
Dim Ones(9) As String
Dim Tens(9) As String
Dim Teens(9) As String

Ones(1) = "One"
Ones(2) = "Two"
Ones(3) = "Three"
Ones(4) = "Four"
Ones(5) = "Five"
Ones(6) = "Six"
Ones(7) = "Seven"
Ones(8) = "Eight"
Ones(9) = "Nine"

Tens(2) = "Twenty"
Tens(3) = "Thirty"
Tens(4) = "Forty"
Tens(5) = "Fifty"
Tens(6) = "Sixty"
Tens(7) = "Seventy"
Tens(8) = "Eighty"
Tens(9) = "Ninety"

Teens(1) = "Eleven"
Teens(2) = "Twelve"
Teens(3) = "Thirteen"
Teens(4) = "Fourteen"
Teens(5) = "Fifteen"
Teens(6) = "Sixteen"
Teens(7) = "Seventeen"
Teens(8) = "Eighteen"
Teens(9) = "Nineteen"

If Val(MyNumber) = 0 Then Exit Function

MyNumber = Right("000" & MyNumber, 3)

' Convert the hundreds place


If Mid(MyNumber, 1, 1) <> "0" Then
Result = Ones(Val(Mid(MyNumber, 1, 1))) & " Hundred "
End If

' Convert the tens place


If Mid(MyNumber, 2, 1) <> "0" Then
If Mid(MyNumber, 2, 1) = "1" And Mid(MyNumber, 3, 1) <> "0" Then
Result = Result & Teens(Val(Mid(MyNumber, 3, 1)))
Exit Function
Else
Result = Result & Tens(Val(Mid(MyNumber, 2, 1))) & " "
End If
End If

' Convert the ones place


If Mid(MyNumber, 3, 1) <> "0" Then
Result = Result & Ones(Val(Mid(MyNumber, 3, 1)))
End If

ConvertHundreds = Trim(Result)
End Function

You might also like