Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 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.

Understanding the Select Case Structure

Written by Allen Wyatt (last updated October 2, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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."

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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 Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Understanding the Select Case Structure.

Author Bio

Allen Wyatt

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. ...

MORE FROM ALLEN

Comparing Documents Top and Bottom

Word has a feature that allows you to compare two documents side-by-side. What if you actually want to compare the ...

Discover More

Quickly Displaying Formatting Specs

It's easy to apply formatting to text, but often hard (after the fact) to know exactly what was done. If you often need ...

Discover More

Last Saved Date in a Footer

When printing out a worksheet, you may want Excel to include, in the footer, the date the data was last saved. There is ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

Discover More

Converting Phone Numbers

Sometimes you receive a phone number that contains alphabetic characters and you need to convert it to a purely numeric ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.