bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Macros > WordBasic Examples > Numbers to Words in WordBasic

Numbers to Words in WordBasic

Summary: Want to spell out your numbers--automatically? This WordBasic macro presents one way to accomplish the task. (This tip works with Microsoft Word 6, and Word 95.)

There are times when it is beneficial, or even mandatory, to spell numbers out. For instance, you may want to spell out "1234" as "one thousand two hundred thirty four." You can do this using some of the field capabilities of Word, but some people don't like to use fields within their documents. The following WordBasic macro, NumberToWords, works quickly and easily to change numbers to words. It is rather long, but it has to do a lot of checking to put together the proper string. It will convert any number between 0 and 999,999. To use it, simply place the insertion point immediately to the right of the number you want to convert. If you place the insertion point in the middle of a number, you will not get the desired results. If you try to convert a number that is too large, or try to run the macro when there is text to the left of the insertion point, then you will get an error.

Sub MAIN
WordLeft 1, 1
Num$ = Selection$()
Num = Val(Num$)
If Left$(Num$, 1) < "0" Or Left$(Num$, 1) > "9" Then Num = - 1
Num$ = ""
Select Case Num
    Case - 1
        Msg$ = "No number to left of insertion point!"
        MsgBox Msg$, "NumberToWords Macro", 48
        CharRight
    Case 0
        Num$ = "Zero"
    Case 1 To 999999
        Num$ = SetThousands$(Num)
    Case Else
        MsgBox "Number too large!", "NumberToWords Macro", 48
        CharRight
End Select
If Num$ > "" Then Insert Num$
End Sub

Function SetOnes$(O)
    Dim Base$(9)
        Base$(1) = "One"
        Base$(2) = "Two"
        Base$(3) = "Three"
        Base$(4) = "Four"
        Base$(5) = "Five"
        Base$(6) = "Six"
        Base$(7) = "Seven"
        Base$(8) = "Eight"
        Base$(9) = "Nine"
    SetOnes$ = Base$(O)
End Function

Function SetTens$(T)
    Dim Tens$(9)
        Tens$(1) = "Ten"
        Tens$(2) = "Twenty"
        Tens$(3) = "Thirty"
        Tens$(4) = "Forty"
        Tens$(5) = "Fifty"
        Tens$(6) = "Sixty"
        Tens$(7) = "Seventy"
        Tens$(8) = "Eighty"
        Tens$(9) = "Ninety"
    Dim Teen$(9)
        Teen$(1) = "Eleven"
        Teen$(2) = "Twelve"
        Teen$(3) = "Thirteen"
        Teen$(4) = "Fourteen"
        Teen$(5) = "Fifteen"
        Teen$(6) = "Sixteen"
        Teen$(7) = "Seventeen"
        Teen$(8) = "Eighteen"
        Teen$(9) = "Nineteen"
    T1 = Int(T / 10)
    T2 = T Mod 10
    A$ = Tens$(T1)
    If(T1 = 1 And T2 > 0) Then
        A$ = Teen$(T2)
    End If
    If(T1 > 1 And T2 > 0) Then
        A$ = A$ + " " + SetOnes$(T2)
    End If
    SetTens$ = A$
End Function

Function SetHundreds$(H)
    H1 = Int(H / 100)
    H2 = H Mod 100
    If H1 > 0 Then A$ = SetOnes$(H1) + " Hundred"
    If H2 > 0 Then
        If A$ > "" Then A$ = A$ + " "
        If H2 < 10 Then A$ = A$ + SetOnes$(H2)
        If H2 > 9 Then A$ = A$ + SetTens$(H2)
    End If
    SetHundreds$ = A$
End Function

Function SetThousands$(Th)
    Th1 = Int(Th / 1000)
    Th2 = Th -(Th1 * 1000)
    If Th1 > 0 Then A$ = SetHundreds$(Th1) + " Thousand"
    If Th2 > 0 Then
        If A$ > "" Then A$ = A$ + " "
        A$ = A$ + SetHundreds$(Th2)
    End If
    SetThousands$ = A$
End Function

Tip #112 applies to Microsoft Word versions: 6 | 95


Find and Replace Almost Anything! An invaluable resource for learning how to harness the full power of Word's search and replace capabilities. You'll discover everything you need in order to master all the intricacies of finding and replacing elements of your document, including the super-powerful "wildcard searches" available in Word.
 
Check out WordTips: Find and Replace today!

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Tips.Net Store

WordTips FAQ
WordTips Premium

Learn Access Now

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

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)