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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I spell out a dollar figure in words?

Status
Not open for further replies.

hleighmc

Programmer
Jul 11, 2001
4
US
In a previous work order, the designer had a cell with this formula in it that worked.."spellnumber(F26)" and it spelled out a dollar figure in words.(i.e. two hundred dollars). However when I try to recreate this work order in a different excel document, this formula does not work even though everything is identical. In the original, I can delete it, retype it and it still works. Does anyone have any idea why or how I can make this work?
 
It should be in a Module. <Alt> <F11> for others who might be &quot;looking in&quot;.

Here's the code if you need it, and again, for anyone else who can use it...

Steps:

1) Hold down <Alt> and hit <F11>

2) Hold down <Ctrl> and hit &quot;R&quot; - to bring up Project Explorer, or from the menu: View - Project Explorer.

3) Also from the menu: Insert - Module

4) From the code below, copy and paste into the Module.


Option Explicit

'================
' Main Function
'================
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count

ReDim Place(9) As String
Place(2) = &quot; Thousand &quot;
Place(3) = &quot; Million &quot;
Place(4) = &quot; Billion &quot;
Place(5) = &quot; Trillion &quot;

'String representation of number
MyNumber = Trim(Str(MyNumber))

'Position of decimal place - 0 if none
DecimalPlace = InStr(MyNumber, &quot;.&quot;)
'Converts cents and sets MyNumber to dollar amount
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & &quot;00&quot;, 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


Then, in any worksheet, you can use the following function...

=spellnumber(cell) - where cell is a reference to the cell address containing the number.

Hope this helps. :)

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top