Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. 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 the If ... End If Structure.
by Allen Wyatt
(last updated November 29, 2014)
Macros in Word are written in a language called Visual Basic for Applications (VBA). Like any other programming language, VBA include certain programming structures used to control how the program executes. One of these structures is the If ... End If structure. The most common use of this structure has the following syntax:
If condition Then ' program statements Else ' program statements End If
When a macro is executing, and this structure is encountered, Word tests whatever condition you have specified following the If keyword. If the condition is true, then the program statements right after the Then keyword are executed. If they are not true, then the statements after the Else keyword are executed. The Else keyword and any following program statements (which together make up an Else clause) are optional; you do not need to include them in your macro.
Regardless of whether the program statements in the If ... End If structure are executed, when Word is done with the structure, the macro continues running with the statement following the End If keyword.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13327) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Understanding the If ... End If Structure.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
Need to know if the user selected some text before running your macro? Here’s how to make that check.Discover More
If you are formatting your document by using a macro, you may need to make some of your text italics. You do that by ...Discover More
An array of variables is a powerful element of VBA programming. If you want to clear what is stored in an array, here's a ...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.