In this Excel VBA Select Case Or Tutorial, you learn how to create an Excel VBA Select Case Or statement to conditionally execute a set of statements based on:
- An expression's value, and
- Whether this value matches any 1 of several values inside a single case expression (Value1 Or Value2 Or … Or Value#).
The Excel VBA Select Case Or snippet template/structure you learn in this Tutorial is a specific application of more general Excel VBA Select Case snippet templates/structures. You may (also) be interested in the following related snippet templates/structures:
- Excel VBA Select Case Between 2 Values.
- Excel VBA Select Case Multiple Values.
- Excel VBA Select Case Multiple Conditions.
- Excel VBA Select Case Multiple Test Expressions.
I link to these Tutorials in the Related Excel Macro and VBA Training Materials and Resources Section.
This Excel VBA Select Case Or Tutorial is accompanied by an Excel workbook with the data and VBA code I use. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Select Case Or Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section.
Table of Contents
Related Excel Macro and VBA Training Materials and Resources
The following Excel Macro and VBA Tutorials may help you better understand and implement this VBA Select Case Or Tutorial.
- Tutorials about general macro and VBA constructs and structures:
- Tutorials for Beginners:
- Excel Macros: Click here to open.
- Excel VBA: Click here to open.
- Enable macros in Excel: Click here to open.
- Work with the Visual Basic Editor (VBE): Click here to open.
- Create Sub procedures: Click here to open.
- Work with:
- Variables: Click here to open.
- Data types: Click here to open.
- Functions: Click here to open.
- Tutorials for Beginners:
- Tutorials with practical VBA applications and macro examples:
- MsgBox: Click here to open.
This Excel VBA Select Case Or Tutorial is part of a more comprehensive series of Excel VBA Select Case Tutorials.
- Excel VBA Select Case Tutorial: Click here to open.
- Excel VBA Select Case Or: Click here to open.
- Excel VBA Select Case And Operator: Click here to open.
- Excel VBA Select Case Multiple Test Expressions: Click here to open.
- Excel VBA Select Case Like Wildcard: Click here to open.
- Excel VBA Select Case Inside For… Next Loop: Click here to open.
- Excel VBA Select Case Range of Cells: Click here to open.
You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives. The following are some of my most popular Excel Tutorials and Training Resources:
- Excel Keyboard Shortcuts Cheat Sheet: Click here to open.
- Work with the Excel XLOOKUP Function: Click here to open.
- Excel Power Query (Get & Transform) Tutorial for Beginners: Click here to open.
If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:
- Premium Courses at the Power Spreadsheets Academy: Click here to open.
- Books at the Power Spreadsheets Library: Click here to open.
- VBA Cheat Sheets: Click here to open.
If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:
- Code generators.
- An extensive code library.
- The ability to create your own code library.
- Advanced coding tools.
If you need help with Excel tasks/projects, you may be interested in working with me: Click here to learn more about working with me.
Excel VBA Select Case Or Snippet Template/Structure
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case TestExpression
Case Case11, Case12, ..., Case1#
CaseStatements1
Case Case21, Case22, ..., Case2#
CaseStatements2
'...
Case Case#1, Case#2, ..., Case##
CaseStatements#
Case Else
ElseStatements
End Select
Step-by-Step Process to Create an Excel VBA Select Case Or Statement
(1) Enter the opening and closing statements of the Select Case statement:
- Select Case.
- End Select.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case '...
'...
End Select
(2) Specify the test expression VBA uses to identify the set of statements to execute.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case TestExpression
'...
End Select
(3) Specify the case expressions used by VBA to identify the set of statements to execute. Each case expression is preceded by the Case keyword.
Use commas to separate multiple:
- Values or strings; or
- Value or string ranges (usually created by working with the To or Is keywords);
Inside a single case expression.
In other words: Use commas (instead of the Or operator) to create an Excel VBA Select Case Or statement that follows the template/structure you learn in this Excel VBA Select Case Or Tutorial.
- Case Case11, Case12, …, Case1#
- Case Case21, Case22, …, Case2#
- …
- Case Case31, Case32, …, Case3#
You can (as a general rule) include as many case expressions as required inside a single Select Case statement.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case TestExpression
Case Case11, Case12, ..., Case1#
'...
Case Case21, Case22, ..., Case2#
'...
'...
Case Case#1, Case#2, ..., Case##
'...
'...
End Select
(4) Specify the set of statements to execute when the test expression (you specified in step #2) matches one of the values or strings (you specified in step #3) inside a case expression.
- CaseStatements1.
- CaseStatements2.
- …
- CaseStatements#.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case TestExpression
Case Case11, Case12, ..., Case1#
CaseStatements1
Case Case21, Case22, ..., Case2#
CaseStatements2
'...
Case Case#1, Case#2, ..., Case##
CaseStatements#
'...
End Select
(5) Specify the set of statements to execute if no value or string (you specified in step #3) inside a case expression matches the test expression (you specified in step #2). These catch-all statements:
- Follow the Case Else keyword; and
- Are optional.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case TestExpression
Case Case11, Case12, ..., Case1#
CaseStatements1
Case Case21, Case22, ..., Case2#
CaseStatements2
'...
Case Case#1, Case#2, ..., Case##
CaseStatements#
Case Else
ElseStatements
End Select
How (and Why) the VBA Select Case Or Statement Works
The Select Case statement does the following:
- Compare a test expression to several case expressions.
- Determine how to proceed based on the case expression that matches the test expression.
You can use commas to separate multiple:
- Values or strings; or
- Value or string ranges (usually created by working with the To or Is keywords);
Inside a single case expression.
If the value returned by the test expression matches any 1 of several (comma-delimited) values inside a single case expression (Case#1, Case#2, …, Case##), the Select Case statement:
- Executes the set of statements associated to the applicable Case clause (whose case expression matches the test expression); and
- Exits the Select Case statement.
From this perspective, commas serve (functionally) a similar purpose to the Or operator.
In other words:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Case#1, Case#2, ..., Case##
Is (in the context of an Excel VBA Select Case Or statement) functionally similar to:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Case#1 Or Case#2 Or ... Or Case##
The Or operator (however) is a logical operator.
If you want to create an Excel VBA Select Case Or statement with the Or operator (instead of the VBA Select Case Or (with commas) statement you learn in previous Sections):
- The test expression; and
- All case expressions;
Must return a Boolean value (True or False) when evaluated. This ensures that (both) the test expression and all case expressions are of the same data type.
The resulting Excel VBA Select Case Or operator snippet template/structure is (therefore) fundamentally different to the Excel VBA Select Case Or (with commas) snippet template/structure you learn in previous Sections. The Excel VBA Select Case Or operator snippet template/structure requires you do the following:
- Set the test expression to the Boolean value True.
- Specify the case expressions as logical expressions (expressions that return True or False). You use logical operators (including Or) to build these logical expressions.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
Select Case True
Case LogicalCaseExpression1
CaseStatements1
Case LogicalCaseExpression2
CaseStatements2
'...
Case LogicalCaseExpression#
CaseStatements#
Case Else
ElseStatements
End Select
In my opinion, the Excel VBA Select Case Or (with commas) snippet template/structure you learn in previous Sections is (as a general rule) more intuitive, readable, and flexible (vs. this alternative Excel VBA Select Case Or operator snippet template/structure).
Excel VBA Select Case Or Example Macro
The VBA Select Case Or example macro (below) displays a message box. The message inside the message box varies, depending on the current month.
I use a VBA Select Case Or statement to specify the proper message.
Sub SelectCaseOr()
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-select-case-or/
'Declare variable to represent message to be displayed in message box
Dim MyMsgBoxMessage As String
'Assign string to the MyMsgBoxMessage variable depending on the current month
Select Case Month(Date:=Date)
Case 1, 2, 3: MyMsgBoxMessage = "It's Q1"
Case 4, 5, 6: MyMsgBoxMessage = "It's Q2"
Case 7, 8, 9: MyMsgBoxMessage = "It's Q3"
Case 10, 11, 12: MyMsgBoxMessage = "It's Q4"
End Select
'Display message box with string held by the MyMsgBoxMessage variable
MsgBox MyMsgBoxMessage
End Sub
The test expression inside the Excel VBA Select Case Or example macro (Month(Date:=Date)) returns an integer between 1 and 12.
The case expressions I specify inside the Excel VBA Select Case Or example macro (considering the above) are:
- 1, 2, 3.
- 4, 5, 6.
- 7, 8, 9.
- 10, 11, 12.
The image below displays the message box shown by Excel when I execute the Excel VBA Select Case Or example macro. I execute this example macro (and create this screenshot) in July.
Download the Excel VBA Select Case Or Example Workbook
This Excel VBA Select Case Or Tutorial is accompanied by an Excel workbook with the data and VBA code I use. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Select Case Or Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section.
Related Excel Macro and VBA Training Materials and Resources
The following Excel Macro and VBA Tutorials may help you better understand and implement this VBA Select Case Or Tutorial.
- Tutorials about general macro and VBA constructs and structures:
- Tutorials for Beginners:
- Excel Macros: Click here to open.
- Excel VBA: Click here to open.
- Enable macros in Excel: Click here to open.
- Work with the Visual Basic Editor (VBE): Click here to open.
- Create Sub procedures: Click here to open.
- Work with:
- Variables: Click here to open.
- Data types: Click here to open.
- Functions: Click here to open.
- Tutorials for Beginners:
- Tutorials with practical VBA applications and macro examples:
- MsgBox: Click here to open.
This Excel VBA Select Case Or Tutorial is part of a more comprehensive series of Excel VBA Select Case Tutorials.
- Excel VBA Select Case Tutorial: Click here to open.
- Excel VBA Select Case Or: Click here to open.
- Excel VBA Select Case And Operator: Click here to open.
- Excel VBA Select Case Multiple Test Expressions: Click here to open.
- Excel VBA Select Case Like Wildcard: Click here to open.
- Excel VBA Select Case Inside For… Next Loop: Click here to open.
- Excel VBA Select Case Range of Cells: Click here to open.
You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives. The following are some of my most popular Excel Tutorials and Training Resources:
- Excel Keyboard Shortcuts Cheat Sheet: Click here to open.
- Work with the Excel XLOOKUP Function: Click here to open.
- Excel Power Query (Get & Transform) Tutorial for Beginners: Click here to open.
If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:
- Premium Courses at the Power Spreadsheets Academy: Click here to open.
- Books at the Power Spreadsheets Library: Click here to open.
- VBA Cheat Sheets: Click here to open.
If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:
- Code generators.
- An extensive code library.
- The ability to create your own code library.
- Advanced coding tools.
If you need help with Excel tasks/projects, you may be interested in working with me: Click here to learn more about working with me.