Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return text from number

Status
Not open for further replies.

xhaya

IS-IT--Management
Jul 23, 2003
34
0
0
TW
Hi does anyone know if there is a std function in excel that returns text given a number?

for example. given the number 53, the function returns "fifty three"

tks
 
No, you'd have to write your own function. If you only want to convert a small range of numbers (like dates in a month) you can write a long 'Select Case' statement, otherwise the only way to do it is to write a clever algorithm that translates it into English. It'll be pretty complicated though.

You could do a search on the web for something like "translate decimal to english", someone has probably already done it. It doesn't matter if it's not in VBA, you can just borrow their logic.

N.
 
Hi xhaya,

The following might be the custom function you're looking for...

It's (currently) set up to display dollars and cents, which is a common situation where numbers need to be converted to "spelled out numbers".

I suspect you might be looking to use this "as is" - i.e. for spelling out currency numbers. If not, you could modify the following code. If you do need to convert the code, please advise, and I or someone else will be pleased to assist you. :)

The code is below... You can (and should - to prevent typos) copy the code from below into a Module.

After copying the code, you can use the function: =spellnumber(A1). If A1 contain 53, then the result will currently be: Fifty Three Dollars and No Cents.

Another option - if you are dealing with currency is to modify the custom function this way...
=" $ "&spellnumber(A1)&" Only", which will give you:
$ Fifty Three Dollars and No Cents Only.

Reminder... In copying this, start with "Option Explicit", and end with "End Function".

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 number
MyNumber = Trim(Str(MyNumber))

'Position of decimal place - 0 if none
DecimalPlace = InStr(MyNumber, ".")
'Converts cents and sets 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 <> &quot;&quot;
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> &quot;&quot; Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = &quot;&quot;
End If
Count = Count + 1
Loop

Select Case Dollars
Case &quot;&quot;
Dollars = &quot;No Dollars&quot;
Case &quot;One&quot;
Dollars = &quot;One Dollar&quot;
Case Else
Dollars = Dollars & &quot; Dollars&quot;
End Select

Select Case Cents
Case &quot;&quot;
Cents = &quot; and No Cents&quot;
Case &quot;One&quot;
Cents = &quot; and One Cent&quot;
Case Else
Cents = &quot; and &quot; & Cents & &quot; Cents&quot;
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(&quot;000&quot; & MyNumber, 3)

'Converts to hundreds place
If Mid(MyNumber, 1, 1) <> &quot;0&quot; Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & &quot; Hundred &quot;
End If

'Converts to tens and ones place
If Mid(MyNumber, 2, 1) <> &quot;0&quot; Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If

GetHundreds = Result
End Function

'===========================================
' Converts number from 10 to 99 into text
'===========================================
Function GetTens(TensText)
Dim Result As String

Result = &quot;&quot; 'null out temporary function value,
If Val(Left(TensText, 1)) = 1 Then 'if value between 10-19
Select Case Val(TensText)
Case 10: Result = &quot;Ten&quot;
Case 11: Result = &quot;Eleven&quot;
Case 12: Result = &quot;Twelve&quot;
Case 13: Result = &quot;Thirteen&quot;
Case 14: Result = &quot;Fourteen&quot;
Case 15: Result = &quot;Fifteen&quot;
Case 16: Result = &quot;Sixteen&quot;
Case 17: Result = &quot;Seventeen&quot;
Case 18: Result = &quot;Eighteen&quot;
Case 19: Result = &quot;Nineteen&quot;
Case Else
End Select
Else 'if value between 20-99
Select Case Val(Left(TensText, 1))
Case 2: Result = &quot;Twenty &quot;
Case 3: Result = &quot;Thirty &quot;
Case 4: Result = &quot;Forty &quot;
Case 5: Result = &quot;Fifty &quot;
Case 6: Result = &quot;Sixty &quot;
Case 7: Result = &quot;Seventy &quot;
Case 8: Result = &quot;Eighty &quot;
Case 9: Result = &quot;Ninety &quot;
Case Else
End Select
Result = Result & GetDigit _
(Right(TensText, 1)) 'get ones place
End If
GetTens = Result
End Function

'===========================================
' Converts number from 1 to 9 into text
'===========================================
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = &quot;One&quot;
Case 2: GetDigit = &quot;Two&quot;
Case 3: GetDigit = &quot;Three&quot;
Case 4: GetDigit = &quot;Four&quot;
Case 5: GetDigit = &quot;Five&quot;
Case 6: GetDigit = &quot;Six&quot;
Case 7: GetDigit = &quot;Seven&quot;
Case 8: GetDigit = &quot;Eight&quot;
Case 9: GetDigit = &quot;Nine&quot;
Case Else: GetDigit = &quot;&quot;
End Select
End Function

I hope this is what you were looking for. :) Please advise.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Nelviticus Dale and Chattin

thanks for the pointers they are very useful indeed
i'll be sure to go thru the code and find out wat i missed

thks again
xhaya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top