0% found this document useful (0 votes)
262 views1 page

Number to Words Conversion Code

This function converts a numeric amount to its word equivalent. It takes in a numeric value and returns a string. It uses arrays to store the values for the tens, teens, hundreds, thousands etc. It has a select case statement to handle the conversion for different number of digits in the input value. For 1, 2 or 3 digits it directly indexes into the arrays, for longer numbers it recursively calls itself to break the number into parts separated by the place values.

Uploaded by

Zaraki7
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views1 page

Number to Words Conversion Code

This function converts a numeric amount to its word equivalent. It takes in a numeric value and returns a string. It uses arrays to store the values for the tens, teens, hundreds, thousands etc. It has a select case statement to handle the conversion for different number of digits in the input value. For 1, 2 or 3 digits it directly indexes into the arrays, for longer numbers it recursively calls itself to break the number into parts separated by the place values.

Uploaded by

Zaraki7
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Function ConvertToWords(ByVal vAmount As Variant) As String

Dim acSelection1() As Variant


Dim acSelection2() As Variant
Dim lcDigit()As Variant
Dim StrCount As Long
Const lcTeens = 2
Const lcHundreds = 3
Const lcThousands = 4
Const lcMillions = 7
Const lcBillions = 10
If vAmount = Empty Then GoTo ExitMe
acSelection1 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen")
acSelection2 = Array("", "", "Twenty-", "Thirty-", "Forty-", "Fifty-", "Sixty-"
, "Seventy-", "Eighty-", "Ninety-")
lcDigit = Array("", "", "", " Hundred ", " Thousand ", "", "", " Million ", "",
"", " Billion ")
StrCount = Len(vAmount)

Select Case StrCount


Case 1: ConvertToWords = acSelection1(Right$(vAmount, 1))
Case 2
If Left(Right$(vAmount, 2), 1) = 1 Then
ConvertToWords = acSelection1(Right$(vAmount, 2))
Else
ConvertToWords = acSelection2(Left(Right$(vAmount, 2), 1)) & acSelection1(Right$
(vAmount, 1))
End If
Case 3: ConvertToWords = acSelection1(Left(Right$(vAmount, 3), 1)) & lcDigit(lcH
undreds) & ConvertToWords(Right(Right$(vAmount, 3), 2))
Case 4 To 5: ConvertToWords = ConvertToWords(Left(vAmount, IIf(StrCount - lcThou
sands = 0, 1, 2))) & lcDigit(lcThousands) & IIf(Left(Right(vAmount, lcHundreds),
1) = 0, ConvertToWords(Right(vAmount, lcTeens)), ConvertToWords(Right(vAmount,
lcHundreds)))
Case 6: ConvertToWords = ConvertToWords(Right$(Left(vAmount, 3), 3)) & lcDigit(l
cThousands) & ConvertToWords(Right(vAmount, 3))
End Select
Exit Function
ExitMe:
ConvertToWords = 0
End Function

You might also like