Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Office 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Understanding Precedence.
by Allen Wyatt
(last updated June 25, 2018)
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 (12960) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Office 365. You can find a version of this tip for the older menu interface of Word here: Understanding Precedence.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!
If you write a macro that makes changes to a document, you may want that macro to save those changes. There are several ...Discover More
When you format a document, you can go so far as to change the font of each character in the document. This may be ...Discover More
When creating a macro, you may need to figure out how many fonts are available to Word. You can do this using the ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.