# Freebee - Numbers as Text

Posted by Rob Jackson on August 09, 2001 8:19 AM

This was a solution to an issue earlier. May be useful to others...

Interesting problem, so I wrote this. It works from 0 to 99999 and all you need to do is add dollars or cents on the end. Just call the first function IntegerToText passing a Long number as the value you want to convert. You will need to call it once for dollar values and once for cent values as it only works on whole numbers.

Function IntegerToText(ByVal Num As Long)
Dim TextNum As String
Dim NumStr As String
Dim LenNum As Integer

If Num &LT; 0 Then
Num = Num * (-1)
End If
TextNum = CStr(Fix(Num))
LenNum = Len(TextNum)

Select Case LenNum
Case 1
NumStr = Xval("0" & TextNum)
Case 2
NumStr = Xval(Right(TextNum, 2))
Case 3
If CInt(Right(TextNum, 2)) = 0 Then
NumStr = Xval("0" & Left(TextNum, 1)) & Units(3)
Else
NumStr = Xval("0" & Left(TextNum, 1)) & Units(3) & "and " & IntegerToText(CInt(Right(TextNum, 2)))
End If
Case 4
If CInt(Right(TextNum, 3)) = 0 Then
NumStr = Xval("0" & Left(TextNum, 1)) & Units(4)
ElseIf CInt(Right(TextNum, 3)) &LT; 100 Then
NumStr = Xval("0" & Left(TextNum, 1)) & Units(4) & "and " & IntegerToText(CInt(Right(TextNum, 3)))
Else
NumStr = Xval("0" & Left(TextNum, 1)) & Units(4) & " " & IntegerToText(CInt(Right(TextNum, 3)))
End If
Case 5
If CInt(Right(TextNum, 3)) = 0 Then
NumStr = Xval(Left(TextNum, 2)) & Units(5)
ElseIf CInt(Right(TextNum, 3)) &LT; 100 Then
NumStr = Xval(Left(TextNum, 2)) & Units(5) & "and " & IntegerToText(CInt(Right(TextNum, 3)))
Else
NumStr = Xval(Left(TextNum, 2)) & Units(5) & " " & IntegerToText(CInt(Right(TextNum, 3)))
End If
Case Else
' Nothing Happens
End Select
IntegerToText = NumStr
End Function

Function Units(ByVal Utype As Integer)
Select Case Utype
Case 1
Units = ""
Case 2
Units = ""
Case 3
Units = "Hundred "
Case 4
Units = "Thousand "
Case 5
Units = "Thousand "
End Select
End Function

Function Xval(ByVal Tval As String)
Select Case Tval
Case "00"
Xval = "Zero "
Case "01"
Xval = "One "
Case "02"
Xval = "Two "
Case "03"
Xval = "Three "
Case "04"
Xval = "Four "
Case "05"
Xval = "Five "
Case "06"
Xval = "Six "
Case "07"
Xval = "Seven "
Case "08"
Xval = "Eight "
Case "09"
Xval = "Nine "
Case "10"
Xval = "Ten "
Case "11"
Xval = "Eleven "
Case "12"
Xval = "Twelve "
Case "13"
Xval = "Thirteen "
Case "14"
Xval = "Fourteen "
Case "15"
Xval = "Fifteen "
Case "16"
Xval = "Sixteen "
Case "17"
Xval = "Seventeen "
Case "18"
Xval = "Eighteen "
Case "19"
Xval = "Nineteen "
End Select

Select Case Left(Tval, 1)
Case "2"
If Right(Tval, 1) = 0 Then
Xval = "Twenty"
Else
Xval = "Twenty - " & Xval("0" & Right(Tval, 1))
End If
Case "3"
If Right(Tval, 1) = 0 Then
Xval = "Thirty"
Else
Xval = "Thirty - " & Xval("0" & Right(Tval, 1))
End If
Case "4"
If Right(Tval, 1) = 0 Then
Xval = "Forty"
Else
Xval = "Forty - " & Xval("0" & Right(Tval, 1))
End If
Case "5"
If Right(Tval, 1) = 0 Then
Xval = "Fifty"
Else
Xval = "Fifty - " & Xval("0" & Right(Tval, 1))
End If
Case "6"
If Right(Tval, 1) = 0 Then
Xval = "Sixty"
Else
Xval = "Sixty - " & Xval("0" & Right(Tval, 1))
End If
Case "7"
If Right(Tval, 1) = 0 Then
Xval = "Seventy"
Else
Xval = "Seventy - " & Xval("0" & Right(Tval, 1))
End If
Case "8"
If Right(Tval, 1) = 0 Then
Xval = "Eighty"
Else
Xval = "Eighty - " & Xval("0" & Right(Tval, 1))
End If
Case "9"
If Right(Tval, 1) = 0 Then
Xval = "Ninety"
Else
Xval = "Ninety - " & Xval("0" & Right(Tval, 1))
End If
End Select
End Function

This can probably be improved upon but it was a interesting challenge and I don't have the time available to refine it further. It will do the job though.

hope its suitable...

Rob

Posted by gregc on August 09, 2001 11:52 AM

Here is one I got off MRExcel awhile ago.

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 &LT;&GT; ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp &LT;&GT; "" 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) &LT;&GT; "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
'Convert the tens and ones place
If Mid(MyNumber, 2, 1) &LT;&GT; "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