
Tips.Net > WordTips Home > Macros > Numbers to Text, Take Three (Over a Million)
Summary: So, you need to convert a number to text that is over one million? Here is a VBA macro that will convert up to 999,999,999. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
In previous issues of WordTips you learned how to use a macro to convert numbers to words. For instance, you can convert 123 to one-hundred twenty-three. The previous incarnations of this macro were limited to numbers below one million. In this version, the ante has been upped a thousand times--this version of the VBA macro will successfully convert numbers up to 999,999,999.
Sub BigCardText()
Dim sDigits As String
Dim sBigStuff As String
sBigStuff = ""
' Select the full number in which the insertion point is located
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdMove
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
' Store the digits in a variable
sDigits = Trim(Selection.Text)
If Val(sDigits) > 999999 Then
If Val(sDigits) <= 999999999 Then
sBigStuff = Trim(Int(Str(Val(sDigits) / 1000000)))
' Create a field containing the big digits and
' the cardtext format flag
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="= " + sBigStuff + " \* CardText", _
PreserveFormatting:=True
' Select the field and copy it
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
sBigStuff = Selection.Text & " million "
sDigits = Right(sDigits, 6)
End If
End If
If Val(sDigits) <= 999999 Then
' Create a field containing the digits and the cardtext format flag
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="= " + sDigits + " \* CardText", _
PreserveFormatting:=True
' Select the field and copy it
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
sDigits = sBigStuff & Selection.Text
' Now put the words in the document
Selection.TypeText Text:=sDigits
Selection.TypeText Text:=" "
Else
MsgBox "Number too large", vbOKOnly
End If
End Sub
You should understand that to use the macro, all you need to do is place the insertion point anywhere within the number you want to convert. You need to make sure, however, that the number does not contain extraneous information, such as dollar signs or commas. When you run BigCardText, the macro checks to see if the selected number is over one million. If it is, it first works on the portion above one million, converting it to words. Then, the value below one million is converted. The final, full wording is put together and pasted back into the document, ready for use.
Tip #1442 applies to Microsoft Word versions: 97 2000 2002 2003
More Power! Expand your skills and make Word really sing! It's all possible with macros. The best resource anywhere for macros is WordTips: The Macros. Check it out today!
Do you want a decade of WordTips information at your fingertips? You can find what you need with the WordTips Ten-Year Library. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips