Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Understanding the Select Case Structure.
by Allen Wyatt
(last updated October 2, 2021)
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 Select Case structure. This structure has the following syntax:
Select Case expression Case expression program statements Case expression program statements Case Else program statements End Select
When a macro is executing and this structure is encountered, Excel uses the expression to test each subsequent Case statement to see if the code under the Case statement should be executed. For instance, consider the following code:
Select Case DayOfWeek Case 1 DayName = "Monday" Case 2 DayName = "Tuesday" Case 3 DayName = "Wednesday" Case 4 DayName = "Thursday" Case 5 DayName = "Friday" Case 6 DayName = "Saturday" Case 7 DayName = "Sunday" Case Else DayName = "Unknown day" End Select
This code assumes you enter it with DayOfWeek already set to a numeric value. Let's say (for example's sake) the value is 4. In this structure, the only code that would be executed is the code under the Case 4 statement—in other words, the macro would set DayName to "Thursday." If DayOfWeek were set to some other value not accounted for by the Case statements (outside of the 1 to 7 range), then the code under Case Else would execute, and the macro would set DayName to "Unknown day."
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11835) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Understanding the Select Case Structure.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Grab some info from a source other than Excel, and you may find the need to delete a certain pattern of rows from a ...Discover More
If you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...Discover More
ISBNs are used to uniquely identify books. You may need to know if an ISBN is valid or not, which involves calculating ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.