Numbers to Words: VBA Code??

K

Khalil Handal

Cell G34 has numeric value (numbers between 100 and 1300), What VBA code
might be used to have this number in words in the cell H34.
i.e.
Cell G34 has the value 561
Cell H34 will show: Five Hundred Sixty One

Any help is appriciated
 
K

Khalil Handal

I tried Bernie Deitrick's UDF in the link provided. It works fine with
English.
With Arabic numbers it seems it is different.
example: 875 is read as: Eight Hundred and Five Seventy: (hundreds "and"
one and tens.
example: 1256 is read as: One Thousand and Two Hundred and Six Fifty
Is there a way to adjust the code to support the Arbic.
I should have this clear from the begining!
 
B

Bernie Deitrick

Khalil,

Pardon my jumping in - I saw my name..... ;-)

Try the code below. This will reverse the ones and tens for the numbers
above 20 - If you need to have, for example, Five and Ten instead of
Fifteen, change the strings in the array declaration:

unitWord = Array("", "one", "two", "three", "four", _
"five", "six", "seven", "eight", _
"nine", "ten", "eleven", "twelve", _
"thirteen", "fourteen", "fifteen", _
"sixteen", "seventeen", "eighteen", "nineteen")

For exampleL

unitWord = Array("", "one", "two", "three", "four", _
"five", "six", "seven", "eight", _
"nine", "ten", "one and ten", "two and ten", _
"three and ten", "four and ten", "five and ten", _
"six and ten", "seven and ten", "eight and ten", "nine
and ten")

mmmm - now I understand where the 'teen' words were derived....

HTH,
Bernie
MS Excel MVP

Function SpellNumber(ByVal n As Double, _
Optional ByVal useword As Boolean = True, _
Optional ByVal ccy As String = "Dollars", _
Optional ByVal cents As String = "", _
Optional ByVal join As String = " And", _
Optional ByVal fraction As Boolean = False) As String
Dim myLength As Long
Dim i As Long
Dim myNum As Long
Dim Remainder As Long

SpellNumber = ""
Remainder = Round(100 * (n - Int(n)), 0)

myLength = Int(Application.Log10(n) / 3)

For i = myLength To 0 Step -1
myNum = Int(n / 10 ^ (i * 3))
n = n - myNum * 10 ^ (i * 3)
If myNum > 0 Then
SpellNumber = SpellNumber & MakeWord(Int(myNum)) & _
Choose(i + 1, "", " thousand ", " million ", " billion ", "
trillion")
End If
Next i
SpellNumber = SpellNumber & IIf(useword, " " & ccy, "") & _
IIf(Remainder > 0, join & " " & Format(Remainder, "00"),
" Only") & _
IIf(fraction, "/100", "") & " " & cents
SpellNumber = Application.Proper(Trim(SpellNumber))

End Function

Function MakeWord(ByVal inValue As Long) As String
Dim unitWord, tenWord
Dim n As Long
Dim unit As Long, ten As Long, hund As Long

unitWord = Array("", "one", "two", "three", "four", _
"five", "six", "seven", "eight", _
"nine", "ten", "eleven", "twelve", _
"thirteen", "fourteen", "fifteen", _
"sixteen", "seventeen", "eighteen", "nineteen")
tenWord = Array("", "ten", "twenty", "thirty", "forty", _
"fifty", "sixty", "seventy", "eighty", "ninety")
MakeWord = ""
n = inValue
If n = 0 Then MakeWord = "zero"
hund = n \ 100
If hund > 0 Then MakeWord = MakeWord & MakeWord(Int(hund)) & " hundred "
n = n - hund * 100
If n < 20 Then
ten = n
MakeWord = MakeWord & unitWord(ten) & " "
Else
ten = n \ 10
unit = n - ten * 10
MakeWord = MakeWord & unitWord(unit) & " "
MakeWord = Trim(MakeWord & tenWord(ten))

End If
MakeWord = Application.Proper(Trim(MakeWord))

End Function
 
G

GB

Pardon me for being curious, but I thought that English numbers are Arabic
ones. I thought the arithmetic system I use was developed by a bunch of Arab
geniuses around 1500-2000 years ago. So, what's the distinction being made
here?
 
B

Bernie Deitrick

GB,

From Khalil's post, it's not the numbers themselves, but the word order when
the numbers are written or spoken.

Bernie
 
B

Bernie Deitrick

You can translate the number into any language required - I helped someone
do an Italian version a long while ago...
 
K

Khalil Handal

Hi, To clarify things.
GB: The numbers we call "Arabic" I think Actually are the "Hindu" being
modified. I remember reading this someplace. You are right: the English
numbers are the Arabic ones.

Bernie:The difference is word order when the numbers are written or spoken.
I used the code you sent but has a: #NAME? error.
I put the nuber 24 in cell C2. In cell D2 had the formula: =spellnumber(C2)
 
B

Bob Phillips

How about this Khalil, does it work for all situations?

Function MakeWord(ByVal inValue As Long) As String
Dim unitWord, tenWord
Dim n As Long
Dim unit As Long, ten As Long, hund As Long

unitWord = Array("", "one", "two", "three", "four", _
"five", "six", "seven", "eight", _
"nine", "ten", "eleven", "twelve", _
"thirteen", "fourteen", "fifteen", _
"sixteen", "seventeen", "eighteen", "nineteen")
tenWord = Array("", "ten", "twenty", "thirty", "forty", _
"fifty", "sixty", "seventy", "eighty", "ninety")
MakeWord = ""
n = inValue
If n = 0 Then MakeWord = "zero"
hund = n \ 100
If hund > 0 Then MakeWord = MakeWord & MakeWord(Int(hund)) & " hundred "
n = n - hund * 100
If n < 20 Then
ten = n
MakeWord = MakeWord & unitWord(ten) & " "
Else
unit = n - ten * 10
MakeWord = Trim(MakeWord & unitWord(unit - (unit \ 10) * 10)) & " "
ten = n \ 10
MakeWord = MakeWord & tenWord(ten)
End If
MakeWord = Application.Proper(Trim(MakeWord))

End Function


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
B

Bob Phillips

Must be Khalil because it works for me. I also looked at Bernie's
suggestion, and although the code is a tad different, the principle is the
same, so it too should work.

It sounds as though you just don't have the SpellNumber function installed
anymore.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
K

Khalil Handal

It worked. Thanks to both of you.

Still I need more options than the one mentioned.
100 --> only one word used "Hundred"
200 --> only one word used "Meatan"
300 --> two words used "three hundred" until we reach 900.
the same goes with 1000's: 1000 --> "thousand", 2000 --> "Alphan".
the tens: 20, 30 until 90. The word is used without "and" ex. 20 --> Twenty.
But with the first digit >0 we must use the "and". ex: 23 --> "three and
twenty"

It seems that it more complicated than the English!!!
 
B

Bernie Deitrick

Khalil,

Probably the simplest fix for this is to check for those cases at the end of
the code. Just after this line

SpellNumber = Application.Proper(Trim(SpellNumber))

insert lines like

SpellNumber = Replace(SpellNumber, "One Hundred", "Hundred")

SpellNumber = Replace(SpellNumber, "Two Hundred", "Meatan")

SpellNumber = Replace(SpellNumber, "One Thousand", "Thousand")

SpellNumber = Replace(SpellNumber, "Two Thousand", "Alphan")

and on, for as many as you need.

Bernie
 
K

Khalil Handal

Thanks a lot.


Bernie Deitrick said:
Khalil,

Probably the simplest fix for this is to check for those cases at the end
of the code. Just after this line

SpellNumber = Application.Proper(Trim(SpellNumber))

insert lines like

SpellNumber = Replace(SpellNumber, "One Hundred", "Hundred")

SpellNumber = Replace(SpellNumber, "Two Hundred", "Meatan")

SpellNumber = Replace(SpellNumber, "One Thousand", "Thousand")

SpellNumber = Replace(SpellNumber, "Two Thousand", "Alphan")

and on, for as many as you need.

Bernie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top