nagpapu

Wednesday, May 07, 2008

Numbers to Words in xls

'
' This function builds on the next one and allows you to
' use it to write cheques etc by formatting a number as
' a currency string
' e.g 101.50 = Rupees One Hundred and one and paise fifty only
'
Function Curr2Words(amt)
'Dim rs As Integer
'Dim ps As Integer
rs = Int(amt)
ps = (amt * 100) Mod 100
If ps = 0 Then
Curr2Words = "Rupees " + Trim(Num2Words(rs)) + " only"
Else
Curr2Words = "Rupees " + Trim(Num2Words(rs)) + " and paise " + Trim(Num2Words(ps)) + " only"
End If
End Function

'
' This function will take a number and return
' a string of characters e.g 101 = "One hundred and one"
' it is very useful for bills etc
'
Function Num2Words(num)
' Application.Volatile
If num > 9999999 Then
Num2Words = "Error!! - Not Supported"
Exit Function
End If
If num < 20 Then
Num2Words = NumLT20(num)
Exit Function
Else
If num >= 100000 Then
If (num Mod 100000) = 0 Then
Num2Words = Num2Words(num \ 100000) + " Lakh"
Exit Function
Else
Num2Words = Num2Words(num \ 100000) + " Lakh " + Num2Words(num Mod 100000)
Exit Function
End If
End If
If num >= 1000 Then
If (num Mod 1000) = 0 Then
Num2Words = Num2Words(num \ 1000) + " Thousand"
Exit Function
Else
Num2Words = Num2Words(num \ 1000) + " Thousand " + Num2Words(num Mod 1000)
Exit Function
End If
End If
If num >= 100 Then
If (num Mod 100) = 0 Then
Num2Words = NumLT20(num \ 100) + " Hundred"
Exit Function
Else
Num2Words = NumLT20(num \ 100) + " Hundred and " + Num2Words(num Mod 100)
Exit Function
End If
Else
If (num Mod 10) = 0 Then
Num2Words = NumTens(num)
Exit Function
Else
Num2Words = NumTens(num) + NumLT20(num Mod 10)
Exit Function
End If
End If
End If
End Function

Private Function NumLT20(num)
Select Case (num Mod 20)
Case 0
NumLT20 = "Zero"
Case 1
NumLT20 = "One"
Case 2
NumLT20 = "Two"
Case 3
NumLT20 = "Three"
Case 4
NumLT20 = "Four"
Case 5
NumLT20 = "Five"
Case 6
NumLT20 = "Six"
Case 7
NumLT20 = "Seven"
Case 8
NumLT20 = "Eight"
Case 9
NumLT20 = "Nine"
Case 10
NumLT20 = "Ten"
Case 11
NumLT20 = "Eleven"
Case 12
NumLT20 = "Twelve"
Case 13
NumLT20 = "Thirteen"
Case 14
NumLT20 = "Fourteen"
Case 15
NumLT20 = "Fifteen"
Case 16
NumLT20 = "Sixteen"
Case 17
NumLT20 = "Seventeen"
Case 18
NumLT20 = "Eighteen"
Case 19
NumLT20 = "Nineteen"
End Select
End Function

Private Function NumTens(num)
Select Case (num \ 10)
Case 2
NumTens = "Twenty "
Case 3
NumTens = "Thirty "
Case 4
NumTens = "Forty "
Case 5
NumTens = "Fifty "
Case 6
NumTens = "Sixty "
Case 7
NumTens = "Seventy "
Case 8
NumTens = "Eighty "
Case 9
NumTens = "Ninety "
End Select
End Function

0 Comments:

Post a Comment

<< Home