Converting Numbers to words

S

Srinivasulu B

I preapre inovices using Excel.
I have to put the total amount in words
I want to convert number to words.
Example
If cell A1 contains134.65 then in another cell (say B1) it should be
Rupees One Hundred Thirty Four and Paise Sixty Five only
(Please note the currency..Rupees, Paise and also "only" at the end)
Can nay one help?
seena
 
J

JLatham

As you are a new user, I thought it might be a little difficult for you to
modify code, so I've done it for you. The following is based on the code at
the Ozgrid site, adapted to provide results as Rupees and Paise.

To get the code into your workbook, open the workbook, press [Alt]+[F11] to
open the VB Editor. In the VB Editor choose Insert and Module. Copy the
code below and paste it into the module presented to youi. Close the VB
Editor.

To use the code, in a worksheet you can put a formula like
=SpellNumber(1234.56)
or even reference a cell that has a number in it like
=SpellNumber(A1)

The code:

'****************
' 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 <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" 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 Rupees"
Case "One"
Dollars = "One Rupee"
Case Else
Dollars = "Rupees " & Dollars
End Select

Select Case Cents
Case ""
Cents = " and No Paise"
Case "One"
Cents = " and One Paise"
Case Else
Cents = " and Paise " & Cents
End Select

SpellNumber = Dollars & Cents & " only"
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) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If

' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "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
 
S

Srinivasulu B

It worked...
Simply wonderful....
I can't thank you enough for this ...Mr.Latham
Thanks again
=================
One more point....
In our country
singular is paisa and
plural is paise
Honestly ....this not at all an issue.
The chances of getting one paisa is virtually nil and even if we get , we
round it off to next closest Rupee.
I am raising this issue just out of acadamic interest (...part of learning
process)
Is it possible to take care of this issue
If it is too involved....forget it
seena


====================================
JLatham said:
As you are a new user, I thought it might be a little difficult for you to
modify code, so I've done it for you. The following is based on the code
at
the Ozgrid site, adapted to provide results as Rupees and Paise.

To get the code into your workbook, open the workbook, press [Alt]+[F11]
to
open the VB Editor. In the VB Editor choose Insert and Module. Copy the
code below and paste it into the module presented to youi. Close the VB
Editor.

To use the code, in a worksheet you can put a formula like
=SpellNumber(1234.56)
or even reference a cell that has a number in it like
=SpellNumber(A1)

The code:

'****************
' 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 <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" 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 Rupees"
Case "One"
Dollars = "One Rupee"
Case Else
Dollars = "Rupees " & Dollars
End Select

Select Case Cents
Case ""
Cents = " and No Paise"
Case "One"
Cents = " and One Paise"
Case Else
Cents = " and Paise " & Cents
End Select

SpellNumber = Dollars & Cents & " only"
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) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If

' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "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



Srinivasulu B said:
I preapre inovices using Excel.
I have to put the total amount in words
I want to convert number to words.
Example
If cell A1 contains134.65 then in another cell (say B1) it should be
Rupees One Hundred Thirty Four and Paise Sixty Five only
(Please note the currency..Rupees, Paise and also "only" at the end)
Can nay one help?
seena
 
J

JLatham

You're welcome, glad it worked as you need.

Srinivasulu B said:
It worked...
Simply wonderful....
I can't thank you enough for this ...Mr.Latham
Thanks again
=================
One more point....
In our country
singular is paisa and
plural is paise
Honestly ....this not at all an issue.
The chances of getting one paisa is virtually nil and even if we get , we
round it off to next closest Rupee.
I am raising this issue just out of acadamic interest (...part of learning
process)
Is it possible to take care of this issue
If it is too involved....forget it
seena


====================================
JLatham said:
As you are a new user, I thought it might be a little difficult for you to
modify code, so I've done it for you. The following is based on the code
at
the Ozgrid site, adapted to provide results as Rupees and Paise.

To get the code into your workbook, open the workbook, press [Alt]+[F11]
to
open the VB Editor. In the VB Editor choose Insert and Module. Copy the
code below and paste it into the module presented to youi. Close the VB
Editor.

To use the code, in a worksheet you can put a formula like
=SpellNumber(1234.56)
or even reference a cell that has a number in it like
=SpellNumber(A1)

The code:

'****************
' 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 <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" 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 Rupees"
Case "One"
Dollars = "One Rupee"
Case Else
Dollars = "Rupees " & Dollars
End Select

Select Case Cents
Case ""
Cents = " and No Paise"
Case "One"
Cents = " and One Paise"
Case Else
Cents = " and Paise " & Cents
End Select

SpellNumber = Dollars & Cents & " only"
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) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If

' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "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



Srinivasulu B said:
I preapre inovices using Excel.
I have to put the total amount in words
I want to convert number to words.
Example
If cell A1 contains134.65 then in another cell (say B1) it should be
Rupees One Hundred Thirty Four and Paise Sixty Five only
(Please note the currency..Rupees, Paise and also "only" at the end)
Can nay one help?
seena
 
S

Shah Shailesh

In case you need to convert as per indian number system (using Lakh,
Crore)you can download one addins "Indian Rupees" from below site.

http://in.geocities.com/shahshaileshs/

Or download one workbook "indianspellnoformula" (scroll to 5) from below
webpage, which has formula to convert numbers to words as per Indian System.
No VBA codes required.

http://in.geocities.com/shahshaileshs/misc.htm


Regards,
Shailesh Shah
http://in.geocities.com/shahshaileshs/
If You Can't Excel with Talent, Triumph with Effort.

Free Addins Office Menu-2003 for Office-2007
http://in.geocities.com/shahshaileshs/menuaddins
 
J

JLatham

Very helpful, I'm certain and I've downloaded the file to examine, and will
remember the location to offer future Indian currency help from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top