convert number to word
The following 2 formulas are from an older version of the "VBA Developers
Handbook" by Ken Getz and Mike Gilbert. I think they'll get you pointed in
the correct direction:
Private Function dhHandleGroup(ByVal intValue As Integer) As String
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
' Called by dhNumToStr
Static varOnes As Variant
Static varTens As Variant
Dim strOut As String
Dim intDigit As Integer
If IsEmpty(varOnes) Then
varOnes = Array("", "One", "Two", "Three", "Four", "Five", _
"Six", "Seven", "Eight", "Nine", "Ten", _
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", _
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty")
End If
If IsEmpty(varTens) Then
' Elements 0 and 1 in this array aren't used.
varTens = Array("", "", "Twenty", "Thirty", "Forty", _
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
End If
' Get the hundreds digit, and then the rest.
intDigit = intValue \ 100
intValue = intValue Mod 100
' If there's a hundreds digit, add that now.
If intDigit 0 Then strOut = varOnes(intDigit) & " Hundred"
' Handle the tens and ones digits.
Select Case intValue
Case 1 To 20
strOut = strOut & varOnes(intValue)
Case 21 To 99
intDigit = intValue \ 10
intValue = intValue Mod 10
If intDigit 0 Then
strOut = strOut & " " & varTens(intDigit)
End If
If intValue 0 Then
strOut = strOut & "-" & varOnes(intValue)
End If
End Select
dhHandleGroup = strOut
End Function
Function dhNumToStr(ByVal varValue As Variant) As String
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
' Takes a number and converts it into text for writing
' checks. For example, 24.95 gets converted to
' Twenty-Four and 95/100
' In:
' varValue contains the number to be converted to text
' Out:
' Returns the string or an empty string on any error
On Error GoTo HandleErrors
Dim intTemp As Integer
Dim varNames As Variant
Dim lngDollars As Long
Dim intCents As Integer
Dim strOut As String
Dim strTemp As String
Dim intI As Integer
If Not IsNumeric(varValue) Then Exit Function
' 999,999,999.99 is the largest possible value.
If varValue 999999999.99 Then Exit Function
varNames = Array("", "Thousand", "Million")
varValue = Abs(varValue)
lngDollars = Int(varValue)
intCents = (varValue - lngDollars) * 100
If lngDollars 0 Then
' Loop through each set of three digits,
' first the hundreds, then thousands, and then
' millions.
Do
intTemp = lngDollars Mod 1000
lngDollars = Int(lngDollars / 1000)
' Prepend spelling of new triplet of digits to the
' existing output.
If intTemp < 0 Then
strOut = dhHandleGroup(intTemp) & " " & _
varNames(intI) & " " & strOut
End If
intI = intI + 1
Loop While lngDollars 0
' Handle the cents.
strOut = RTrim(strOut) & " and " & _
Format$(intCents, "00") & "/100"
End If
ExitHe
dhNumToStr = strOut
Exit Function
HandleErrors:
' Handle all errors by returning an empty string
strOut = ""
Resume ExitHere
End Function
--
Kevin Backmann
"ROHIT" wrote:
I wont to convert Currancy to Word.
Example : 567 = Five Hundrad Sisty Saven
Thanking you
|