
Tips.Net > WordTips Home > Creating Macros > Variables > Converting Numbers to Strings
Summary: One of the most common data conversions done by programmers is to convert numeric values to their string equivalent. This tip explains how you can do the conversion in your own macros. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
You already know that you can use variables in your macros, and that there are two very basic types of variables: string variables (containing characters) and numeric variables (containing numeric values). You can quickly and easily convert a number into a string. This is the done with the Str() function. The syntax for using this function is:
A = Str(B)
In this syntax, if B is a numeric value equal to 5, then when completed, the string in A will be " 5"; if B is -4, then A would be "-4". Notice the leading space when converting positive numbers. This may not provide satisfactory results for some subroutines. Instead, you should create a function that returns a stripped-down version of the string. The following function does just that:
Function ToNum(X as Variant) as String
Dim A as String
A = Ltrim(Str(X))
ToNum = A
End Function
The reason that the value passed to the function (X) is defined as a Variant is that you can then pass any type of numeric value.
Tip #1093 applies to Microsoft Word versions: 97 2000 2002 2003
Step Up and Take Control! Subscribers to WordTips know just how valuable a resource it is. WordTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on WordTips archives and e-books.
Check out WordTips Premium today!
Thousands of WordTips, available for immediate download. Have all the Microsoft Word info you need, right at your fingertips. (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