How To Create The Sample Function Called SpellNumber
How To Create The Sample Function Called SpellNumber
Option Explicit
'Main Function
Function SpellNumber(ByVal MyNumber)
Dim Pesos, Centavos, 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 Centavos and set MyNumber to peso amount.
If DecimalPlace > 0 Then
Centavos = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Pesos = Temp & Place(Count) & Pesos
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Pesos
Case ""
Pesos = "No Pesos"
Case "One"
Pesos = "One Peso"
Case Else
Pesos = Pesos & " Pesos"
End Select
Select Case Centavos
Case ""
Centavos = " and No Centavos"
Case "One"
Centavos = " and One Cent"
Case Else
Centavos = " and " & Centavos & " Centavos"
End Select
SpellNumber = Pesos & Centavos
End Function
To use the sample functions to change a number to written text, use one of the methods
demonstrated in the following examples:
You can change 32.50 into "Thirty Two Pesos and Fifty Centavos" by entering the following
formula into a cell:
=SpellNumber(32.50)
You can refer to other cells in the workbook. For example, enter the number 32.50 into cell A1,
and type the following formula into another cell:
=SpellNumber(A1)
To enter a custom function into a worksheet, you can use Paste Function in Excel 2000, or you
can use Insert Function in Excel 2002 and in Excel 2003.