Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Understanding Precedence.
by Allen Wyatt
(last updated February 28, 2019)
Macros often contain mathematical formulas. Those formulas can sometimes become quite complex. Any time a formula contains more than one operator (such as + or -), VBA needs to figure out in which order to perform the operations. This is where the concept of precedence comes into play. Precedence is the built-in macro logic that determines the order in which the operations occur. This is the normal order:
|* and /||Multiplication and division|
|+ and -||Addition and subtraction|
Notice that there are two entries in the precedence list in which there are two operators. Multiplication and division are at the same precedence level, as are addition and subtraction. If a formula involves two or more operators at the same precedence level, then the operators are evaluated from left to right within the formula.
If you want to change the order in which a formula is evaluated, you need to use parentheses. Operations within parentheses are evaluated before those outside of them. Thus, a formula such as 3 * (4 + 2) would produce a result of 18 because the addition within the parentheses takes precedence over the multiplication outside the parentheses. If the parentheses had been omitted, then 3 * 4 + 2 would have resulted in 14.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1452) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Understanding Precedence.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
You may need to determine the numeric value of a character in a macro. You can do that using the Asc function, described ...Discover More
Need to do some macro processing of documents in the user's My Documents folder? First step is to figure out where the ...Discover More
Macros can make your use of Word faster and easier than ever before. What do you do, however, when you try to save a ...Discover More
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.