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

Understanding the Select Case Structure

Written by Allen Wyatt (last updated December 19, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


3

Macros in Word are written in a language called VBA. Like any other programming language, VBA includes certain programming structures that 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, Word uses the expression (first line) 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."

You also are not limited to simple Case statement expressions. For instance, you could use a Select Case structure such as the following:

Select Case DayOfWeek
    Case 1 To 5
        DayName = "Weekday"
    Case 6, 7
        DayName = "Weekend"
    Case Else
        DayName = "Unknown day"
End Select

Note the use of the range in the first Case statement and the use of multiple values in the second. The Case statement expression is quite flexible in how you can structure it.

Note:

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

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12692) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word 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

Don't Update Links to Other Programs

If you have links in your workbook to data in other workbooks, you may want to control whether Excel updates those links ...

Discover More

Specifying Paper Trays for Specific Pages in a Single Print Job

If your printer has multiple paper trays, you may want to send some pages to one tray and other pages to a different ...

Discover More

Error Generated when Trying to Copy a Worksheet

How successful you are in copying information in Excel depends on lots of issues. This tip examines how those issues can ...

Discover More

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!

More WordTips (ribbon)

Determining the Current Page Number

While your macro is processing the text in your document, you may need a way to determine the current page number where ...

Discover More

Defining a Shortcut for a Macro

You can make running macros very easy if you assign a shortcut key to the macro. This tip demonstrates how easy it is to ...

Discover More

Clean Up Your Macro List

Macros are part of the document and template that are loaded into memory when you open a file. If you have "junk" macros ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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 2 + 2?

2019-04-07 03:22:30

Ron S

Thanks Allen.

That is a bigger test than I tried. Dedication. I was hoping to find a documented limit. But logically, there is no reason for a limit because there is no structure that needs to be kept in memory. The Select, identifies the start and End Select and VBA just has to handle one case at a time


2019-04-06 08:38:48

Allen

Good question, Ron. I don't know of an upper limit on the number of Case statements you can have within a Select Case structure. I just put together one that has 1001 levels (1000 explicit and 1 Case Else) and it worked fine. (I stopped at 1000 because I figured it really didn't matter at that point -- the structure was way big enough.)

-Allen


2019-04-06 05:06:44

Ron

Is there an upper limit to the number cases that can be evaluated? I've searched but have not found any documentation for that.


This Site

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.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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.