0% found this document useful (0 votes)
5 views

Function NumberToWords

The document provides two VBA functions for Excel: one for converting whole numbers to words and another for converting decimal numbers to words. Both functions utilize arrays for units and tens, and handle the conversion of numbers into their word equivalents, appending a specified currency name at the end. The functions include a helper function to convert hundreds and manage both integer and decimal parts of the input number.

Uploaded by

subhan mba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Function NumberToWords

The document provides two VBA functions for Excel: one for converting whole numbers to words and another for converting decimal numbers to words. Both functions utilize arrays for units and tens, and handle the conversion of numbers into their word equivalents, appending a specified currency name at the end. The functions include a helper function to convert hundreds and manage both integer and decimal parts of the input number.

Uploaded by

subhan mba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Change Number to words For Whole Number, the Correct VBA for Excel:

Function NumberToWords(ByVal MyNumber)

Dim UnitsArray As Variant

Dim TensArray As Variant

Dim Place(5) As String

Dim Temp As String

Dim DecimalPlace As Integer

Dim Count As Integer

Dim CurrencyName As String

' Define the currency name and add " only" at the end

CurrencyName = " Afghani Only" ' Change this to your preferred currency

' Arrays for number conversion

UnitsArray = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", _

"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", _

"Eighteen", "Nineteen")

TensArray = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")

Place(2) = " Thousand "

Place(3) = " Million "

Place(4) = " Billion "

Place(5) = " Trillion "

' Convert MyNumber to string and find decimal place

MyNumber = Trim(CStr(MyNumber))

DecimalPlace = InStr(MyNumber, ".")

If DecimalPlace > 0 Then


MyNumber = Left(MyNumber, DecimalPlace - 1)

End If

Count = 1

Do While MyNumber <> ""

Temp = GetHundreds(Right(MyNumber, 3))

If Temp <> "" Then NumberToWords = Temp & Place(Count) & NumberToWords

If Len(MyNumber) > 3 Then

MyNumber = Left(MyNumber, Len(MyNumber) - 3)

Else

MyNumber = ""

End If

Count = Count + 1

Loop

' Add currency and "only" at the very end

NumberToWords = Application.Trim(NumberToWords) & CurrencyName

End Function

Function GetHundreds(ByVal MyNumber)

Dim Result As String

Dim UnitsArray As Variant

Dim TensArray As Variant

' Arrays for number conversion

UnitsArray = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", _

"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", _

"Eighteen", "Nineteen")
TensArray = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")

If Val(MyNumber) = 0 Then Exit Function

MyNumber = Right("000" & MyNumber, 3)

' Convert the hundreds place

If Mid(MyNumber, 1, 1) <> "0" Then

Result = UnitsArray(Mid(MyNumber, 1, 1)) & " Hundred "

End If

' Convert the tens and units place

If Mid(MyNumber, 2, 1) = "1" Then

Result = Result & UnitsArray(Val(Mid(MyNumber, 2)))

Else

Result = Result & TensArray(Mid(MyNumber, 2, 1))

Result = Result & " " & UnitsArray(Mid(MyNumber, 3, 1))

End If

GetHundreds = Trim(Result)

End Function
Change Number to words For Decimal Number, the Correct VBA for Excel:
Function NumberToWords(ByVal MyNumber)

Dim UnitsArray As Variant

Dim TensArray As Variant

Dim Place(5) As String

Dim Temp As String

Dim DecimalPlace As Integer

Dim Count As Integer

Dim CurrencyName As String

Dim IntegerPart As String

Dim DecimalPart As String

' Define the currency name

CurrencyName = " Afghani Only" ' Change this to your preferred currency

' Arrays for number conversion

UnitsArray = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", _

"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", _

"Eighteen", "Nineteen")

TensArray = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")

Place(2) = " Thousand "

Place(3) = " Million "

Place(4) = " Billion "

Place(5) = " Trillion "

' Convert MyNumber to string and find decimal place

MyNumber = Trim(CStr(MyNumber))
DecimalPlace = InStr(MyNumber, ".")

' Split the number into integer and decimal parts

If DecimalPlace > 0 Then

IntegerPart = Left(MyNumber, DecimalPlace - 1)

DecimalPart = Mid(MyNumber, DecimalPlace + 1)

Else

IntegerPart = MyNumber

DecimalPart = ""

End If

' Convert the integer part to words

Count = 1

Do While IntegerPart <> ""

Temp = GetHundreds(Right(IntegerPart, 3))

If Temp <> "" Then NumberToWords = Temp & Place(Count) & NumberToWords

If Len(IntegerPart) > 3 Then

IntegerPart = Left(IntegerPart, Len(IntegerPart) - 3)

Else

IntegerPart = ""

End If

Count = Count + 1

Loop

' Add currency name for the integer part

NumberToWords = Application.Trim(NumberToWords)

' Handle the decimal part

If DecimalPart <> "" Then


Dim DecimalResult As String

Dim i As Integer

For i = 1 To Len(DecimalPart)

Dim Digit As Integer

Digit = Val(Mid(DecimalPart, i, 1))

If Digit > 0 Then

DecimalResult = DecimalResult & " " & UnitsArray(Digit)

End If

Next i

NumberToWords = NumberToWords & " Point" & DecimalResult

End If

' Add "dollars only" at the end

NumberToWords = NumberToWords & CurrencyName

End Function

Function GetHundreds(ByVal MyNumber)

Dim Result As String

Dim UnitsArray As Variant

Dim TensArray As Variant

' Arrays for number conversion

UnitsArray = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", _

"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", _

"Eighteen", "Nineteen")

TensArray = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")

If Val(MyNumber) = 0 Then Exit Function


MyNumber = Right("000" & MyNumber, 3)

' Convert the hundreds place

If Mid(MyNumber, 1, 1) <> "0" Then

Result = UnitsArray(Mid(MyNumber, 1, 1)) & " Hundred "

End If

' Convert the tens and units place

If Mid(MyNumber, 2, 1) = "1" Then

Result = Result & UnitsArray(Val(Mid(MyNumber, 2)))

Else

Result = Result & TensArray(Mid(MyNumber, 2, 1))

Result = Result & " " & UnitsArray(Mid(MyNumber, 3, 1))

End If

GetHundreds = Trim(Result)

End Function

You might also like