Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Understanding the If ... End If Structure.
Macros in Excel are written in a language called Visual Basic for Applications (VBA). Like any other programming language, VBA includes certain programming structures which are 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, Excel tests whatever condition you have defined. 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 Excel is done with the structure, the macro continues running with the statement following the End If keyword.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2023) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Understanding the If ... End If Structure.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!