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 Select Case Structure.

Understanding the Select Case Structure

by Allen Wyatt
(last updated October 26, 2013)

2

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

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, and 2013. 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

Quick AutoFill Variations

The AutoFill feature can be used for more than just incrementing information into cells. This tip explains how to access the ...

Discover More

Word Link to Create a New Excel Workbook

It's easy to create and include links in your documents to other sources, in and out of Word. There are some limitations you ...

Discover More

Positioning the Cursor in a New Document

Creating special templates is a great way to establish "standards" for your documents. With a little ingenuity you can even ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Counting Open Document Windows

When creating macros, it is sometimes necessary to know how many documents are open in Word. This is relatively easy to do ...

Discover More

Counting the Instances of a Text String

Sometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a count, ...

Discover More

Converting Inches to Points

Typographical measurements are often expressed in points. There are several formatting settings that, when accessed through a ...

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}] 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 + 1?

2014-04-21 04:42:51

Kem

I've created a userform with 10 optionbuttons. Can you help me use select case to determine which optionbutton has the value (instead of ten times an 'end if" construction)?
If an optionbutton has the value true, then some text boxes need to be set visible.

Thank you.
Kem


2013-10-28 07:27:16

Bryan

You aren't stuck with simple expressions, either. Here are three other ways you can use the Case statement:

Select Case DayOfWeek
Case 1 to 5
' This statement fires for any DayOfWeek between 1 and 5
DayName = "Week Day"
Case Is > 5
' This number fires for any DayOfWeek greater than 5
DayName = "Weekend"
End Select

or

Select Case DayOfWeek
Case 1, 3, 5, 7
' Any odd number
DayName = "Odd Day"
Case 2, 4, 6
' Any Even number
DayName = "Even Day"
End Select

The DayOfWeek used in these examples could be a constant, a variable, or even a function.


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.

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