In this Excel VBA Select Case Like Wildcard Tutorial, you learn how to create an Excel VBA Select Case Like wildcard statement to conditionally execute a set of statements based on an expression's value, while:
- Working with the Like operator; and
- Considering wildcards.
This Excel VBA Select Case Like Wildcard 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 Like Wildcard 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 Like Wildcard 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 Like Wildcard 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 Like Wildcard Snippet Template/Structure
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/select-case-like-wildcard/
Select Case True
Case String1 Like Pattern1
CaseStatements1
Case String2 Like Pattern2
CaseStatements2
'...
Case String# Like Pattern#
CaseStatements#
Case Else
ElseStatements
End Select
Step-by-Step Process to Create an Excel VBA Select Case Like Wildcard 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/select-case-like-wildcard/
Select Case '...
'...
End Select
(2) Set the test expression VBA uses to identify the set of statements to execute to True.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/select-case-like-wildcard/
Select Case True
'...
End Select
(3) Specify the logical case expressions used by VBA to identify the set of statements to execute.
To create a VBA Select Case Like wildcard statement, specify case expressions as logical expressions that work with:
- The Like operator; and
- Wildcards.
Logical expressions return a Boolean value (True or False) when evaluated. The Like operator compares a string against a pattern. You can use wildcards when specifying this pattern.
Each logical case expression is preceded by the Case keyword.
- Case String1 Like Pattern1.
- Case String2 Like Pattern2.
- …
- Case String# Like Pattern#.
Use the following when specifying a pattern:
- Wildcard characters.
- A question mark (?) in the pattern matches any single character.
- An asterisk (*) matches any sequence of characters.
- A hash sign (#) matches any single digit between 0 and 9.
- Character lists or character ranges.
- “[CharacterList]” matches any character included in CharacterList.
- “[!CharacterList]” matches any character not included in CharacterList.
- Use the hyphen (-) to specify a character or digit range.
- Square brackets are (also) escape characters. You can wrap (most of) the special characters I list above (?, *, #, [) in square brackets ([SpecialCharacter]) to match the (literal) applicable special character.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/select-case-like-wildcard/
Select Case True
Case String1 Like Pattern1
'...
Case String2 Like Pattern2
'...
'...
Case String# Like Pattern#
'...
'...
End Select
(4) Specify the set of statements to execute when the applicable logical case expression with the Like operator (you specified in step #3) returns True.
- CaseStatements1.
- CaseStatements2.
- …
- CaseStatements#.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/select-case-like-wildcard/
Select Case True
Case String1 Like Pattern1
CaseStatements1
Case String2 Like Pattern2
CaseStatements2
'...
Case String# Like Pattern#
CaseStatements#
'...
End Select
(5) Specify the set of statements to execute if no logical case expression with the Like operator (you specified in step #3) returns True. These catch-all statements:
- Follow the Case Else keyword; and
- Are optional.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/select-case-like-wildcard/
Select Case True
Case String1 Like Pattern1
CaseStatements1
Case String2 Like Pattern2
CaseStatements2
'...
Case String# Like Pattern#
CaseStatements#
Case Else
ElseStatements
End Select
How (and Why) the VBA Select Case Like Wildcard 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.
The Like operator is a logical operator. An expression with the Like operator returns a Boolean value (True or False) when evaluated.
When you create an Excel VBA Select Case Like wildcard statement:
- 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.
To achieve this, do the following inside the applicable VBA Select Case Like wildcard statement:
- Set the test expression to the Boolean value True.
- Specify the case expressions as logical expressions that work with:
- The Like operator; and
- Wildcards.
When a case expression returns True, the Select Case statement:
- Executes the set of statements associated to the applicable Case clause; and
- Exits the Select Case statement.
Excel VBA Select Case Like Wildcard Example Macro
The VBA Select Case Like wildcard example macro (below) displays a message box. The message inside the message box varies, depending on the current month's name.
I use a VBA Select Case Like wildcard statement to specify the proper message.
Sub SelectCaseLikeWildcard()
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/select-case-like-wildcard/
'Declare variables to represent:
'Message to be displayed in message box
'Current month's name
Dim MyMsgBoxMessage As String
Dim MyCurrentMonthName As String
'Obtain current month's name
MyCurrentMonthName = MonthName(Month(Date:=Date))
'Assign a string to the MyMsgBoxMessage variable depending on pattern followed by the current month's name
Select Case True
Case MyCurrentMonthName Like "*ary"
MyMsgBoxMessage = "It's January or February"
Case MyCurrentMonthName Like "Ju??"
MyMsgBoxMessage = "It's June or July"
Case MyCurrentMonthName Like "*ber"
MyMsgBoxMessage = "It's September, October, November, or December"
Case Else
MyMsgBoxMessage = "It's March, April, May, or August"
End Select
'Display message box with string held by the MyMsgBoxMessage variable
MsgBox MyMsgBoxMessage
End Sub
The VBA Select Case Like wildcard statement inside the VBA Select Case Like wildcard example macro specifies 3 case expressions with:
- The Like operator; and
- Wildcards.
The patterns I use inside these case expressions are as follows:
- “*ary” matches:
- Any sequence of characters (*);
- Where the last 3 letters are “ary”.
- “Ju??” matches a sequence of 4 characters, where the first 2 letters are “Ju”.
- “*ber” matches:
- Any sequence of characters (*);
- Where the last 3 letters are “ber”.
The image below displays the message box shown by Excel when I execute the Excel VBA Select Case Like wildcard example macro. I execute this example macro (and create this screenshot) in June.
Download the Excel VBA Select Case Like Wildcard Example Workbook
This Excel VBA Select Case Like Wildcard 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 Like Wildcard 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 Like Wildcard 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 Like Wildcard 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.