Macro Excel: Number to Word conversion for various purpose in English

The use ms excel to complete office work or personal work is often required to change a number into words. Here are examples of programs VBA (Visual Basic Application) that can be added to your sheet as a function that can be used at any time with ease.
There are notes on the writing of symbols, for example if the number of money units will need to be replaced by the reference number. Units can be changed be anything as you want, for example U.S. currency with the dollar, the currency in Indonesia Rupiah, the Japanese currency to yen, and so on. In addition to the currency can also be changed with another unit for example: Kilogram to load, Meter for distance, etc..

==================== start of script ========================

Function toword(ByVal Ninput)

Dim Temp
Dim Cnum, Cents
Dim DecimalPlace, Count
Dim onecurrency, morecurrency

‘ Your Country  currency or other unity you need
‘ if without currency or unity fill variabels bellow in blank’
‘ =====================================================================
‘ onecurrency = “Rupiah” ‘Indonesian’
‘ morecurrency = “Rupiahs”
‘ onecurrency = “Dollar” ‘Dollar
‘ morecurrency = “Dollars”
onecurrency = “Kilogram” ‘weight
morecurrency = “Kilograms”
‘ =====================================================================

ReDim Place(9) As String

Place(2) = ” Thousand ”

Place(3) = ” Million ”

Place(4) = ” Billion ”

Place(5) = ” Trillion ”

‘ Convert Ninput to a string, trimming extra spaces.

Ninput = Trim(Str(Ninput))

‘ Find decimal place.

DecimalPlace = InStr(Ninput, “.”)

‘ If we find decimal place…

If DecimalPlace > 0 Then

‘ Convert cents

Temp = Left(Mid(Ninput, DecimalPlace + 1) & “00”, 2)

Cents = ConvertTens(Temp)

‘ Strip off cents from remainder to convert.

Ninput = Trim(Left(Ninput, DecimalPlace – 1))

End If

Count = 1

Do While Ninput <> “”

Temp = ConvertHundreds(Right(Ninput, 3))

If Temp <> “” Then Cnum = Temp & Place(Count) & Cnum

If Len(Ninput) > 3 Then

‘ Remove last 3 converted digits from Ninput.

Ninput = Left(Ninput, Len(Ninput) – 3)

Else

Ninput = “”

End If

Count = Count + 1

Loop

‘ Check number input.

Select Case Cnum

Case “”

Cnum = “No ”

Case “One”

Cnum = “One” & ” ” & onecurrency

Case Else

Cnum = Cnum & ” ” & morecurrency

End Select

‘ check cents

Select Case Cents

Case “”

Cents = “”

Case “One”

Cents = ” And One Cent”

Case Else

Cents = ” And ” & Cents & ” Cents”

End Select

toword = Cnum & Cents

End Function

Private Function ConvertHundreds(ByVal MyNumber)

Dim Result As String

‘ Exit if there is nothing to convert.

If Val(MyNumber) = 0 Then Exit Function

‘ Append leading zeros to number.

MyNumber = Right(“000” & MyNumber, 3)

‘ Do we have a hundreds place digit to convert?

If Left(MyNumber, 1) <> “0” Then

Result = ConvertDigit(Left(MyNumber, 1)) & ” Hundred ”

End If

‘ Do we have a tens place digit to convert?

If Mid(MyNumber, 2, 1) <> “0” Then

Result = Result & ConvertTens(Mid(MyNumber, 2))

Else

‘ If not, then convert the ones place digit.

Result = Result & ConvertDigit(Mid(MyNumber, 3))

End If

ConvertHundreds = Trim(Result)

End Function

Private Function ConvertTens(ByVal MyTens)

Dim Result As String

‘ Is value between 10 and 19?

If Val(Left(MyTens, 1)) = 1 Then

Select Case Val(MyTens)

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

‘ .. otherwise it’s between 20 and 99.

Select Case Val(Left(MyTens, 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

‘ Convert ones place digit.

Result = Result & ConvertDigit(Right(MyTens, 1))

End If

ConvertTens = Result

End Function

Private Function ConvertDigit(ByVal MyDigit)

Select Case Val(MyDigit)

Case 1: ConvertDigit = “One”

Case 2: ConvertDigit = “Two”

Case 3: ConvertDigit = “Three”

Case 4: ConvertDigit = “Four”

Case 5: ConvertDigit = “Five”

Case 6: ConvertDigit = “Six”

Case 7: ConvertDigit = “Seven”

Case 8: ConvertDigit = “Eight”

Case 9: ConvertDigit = “Nine”

Case Else: ConvertDigit = “”

End Select

End Function

======================end of script=====================

source: http://www.ozgrid.com/VBA/CurrencyToWords.htm with any modifications.

About webadm