Table of Contents
Wildcards in VBA (Visual Basic for Applications) are special characters that can be used as placeholders or symbols to represent one or more characters in a string of text. They are commonly used in programming to search for and manipulate data that may vary in its exact format or spelling.
In VBA, wildcards are primarily used in the context of the “Like” operator, which is used to compare strings of text. The asterisk symbol (*) is the most commonly used wildcard and represents any number of characters, including none. For example, the pattern “apple*” would match strings such as “apple”, “apples”, “apple pie”, “apple juice”, etc.
Another useful wildcard is the question mark symbol (?), which represents a single character. This can be helpful when searching for strings with variable spelling or spacing, such as “color” vs “colour” or “John” vs “Jon”.
Wildcards can also be combined to create more specific patterns. For instance, the pattern “a*e” would match strings such as “ace”, “age”, “ape”, “axe”, etc.
Some other examples of using wildcards in VBA include:
1. Searching for all files with a specific file extension, such as “*.xlsx” to find all Excel files in a folder.
2. Checking if a string contains certain characters or follows a certain format, such as “###-##-####” to represent a social security number.
3. Validating user input, such as requiring a certain number or type of characters in a password.
In summary, wildcards in VBA provide a powerful and flexible way to search for and manipulate data, making it easier to handle variations and inconsistencies in text.
Use Wildcards in VBA (With Examples)
You can use the Like operator in VBA along with the following built-in wildcard characters to search for specific patterns in strings:
- * : Matches any number of characters
- ? : Matches a single character
- # : Matches a single digit
- [] : Matches any character in range
The following examples show how to use these wildcard characters in practice.
Example 1: Use * Wildcard To Search for Substring
Suppose we have the following list of foods in column A:

We can create the following macro to search for the substring “hot” in each string in column A and output the results in column B:
Sub FindString()
Dim i As Integer
For i = 2 To 10
If Range("A" & i) Like "*hot*" Then
Range("B" & i) = "Contains hot"
Else
Range("B" & i) = "Does Not Contain hot"
End IfNext i
End Sub
When we run this macro, we receive the following output:

Example 2: Use * Wildcard To Search for Strings that End in Pattern
Suppose we have the following list of basketball team names in column A:

We can create the following macro to search for strings in column A that end in “ets” and output the results in column B:
Sub FindEndingString()
Dim i As Integer
For i = 2 To 10
If Range("A" & i) Like "*ets" Then
Range("B" & i) = "Ends in ets"
Else
Range("B" & i) = "Does Not End in ets"
End IfNext i
End Sub
When we run this macro, we receive the following output:

Example 3: Use # Wildcard To Search for Numbers
Suppose we have the following list of strings in column A:

We can create the following macro to search for strings in column A that contain numbers and output the results in column B:
Sub FindNumbers()
Dim i As Integer
For i = 2 To 10
If Range("A" & i) Like "*#*" Then
Range("B" & i) = "Contains Numbers"
Else
Range("B" & i) = "Does Not Contain Numbers"
End IfNext i
End Sub
When we run this macro, we receive the following output:

Example 4: Use [] Wildcard To Search for Several Characters
Suppose we have the following list of basketball team names in column A:

We can create the following macro to search for strings in column A that contain an r, s or t somewhere in the team name and output the results in column B:
Sub FindSpecificLetters()
Dim i As Integer
For i = 2 To 10
If Range("A" & i) Like "*[r-t]*" Then
Range("B" & i) = "Contains r, s, or t"
Else
Range("B" & i) = "Does Not Contain r, s or t"
End IfNext i
End Sub
When we run this macro, we receive the following output:

Note: You can find the complete documentation for the VBA wildcard characters .
The following tutorials explain how to perform other common tasks using VBA:
Cite this article
stats writer (2024). How can I use wildcards in VBA and what are some examples of their usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-wildcards-in-vba-and-what-are-some-examples-of-their-usage/
stats writer. "How can I use wildcards in VBA and what are some examples of their usage?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-wildcards-in-vba-and-what-are-some-examples-of-their-usage/.
stats writer. "How can I use wildcards in VBA and what are some examples of their usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-wildcards-in-vba-and-what-are-some-examples-of-their-usage/.
stats writer (2024) 'How can I use wildcards in VBA and what are some examples of their usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-wildcards-in-vba-and-what-are-some-examples-of-their-usage/.
[1] stats writer, "How can I use wildcards in VBA and what are some examples of their usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use wildcards in VBA and what are some examples of their usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
