Professional Documents
Culture Documents
VBA Like Operator (Easy Examples) - How To Use 'Like' in Excel VBA
VBA Like Operator (Easy Examples) - How To Use 'Like' in Excel VBA
VBA Like
Article by
Jeevan A Y
Reviewed by
Dheeraj Vaidya, CFA, FRM
“LIKE” operator is the most underused operator despite its wonderful usage. I have not
seen many people who use this operator to a full extent in their coding. In fact, I am
one of them who doesn’t use this operator quite often. “VBA LIKE” operator allows us
to match the pattern of the string against the full string. By using VBA LIKE operator,
we can compare two strings against the patter given. We can check whether the string
contains a substring in VBASubstring In VBAVBA SubString is a crucial function
used for splitting the data by dividing a VBA string into different substrings.
There are three types of substring functions available in VBA, i.e., left-right, mid
and split functions.read more, or we can also whether the string contains any
specific format. If the pattern matches with the string, then VBA LIKE operator returns
TRUE or else FALSE.
While matching strings, we need to use wildcard characters to the patter we specify.
Cookies help us provide, protect and improve our products and services. By using our website, you agree to our use of
Below are the wildcards we use in VBA LIKE operator.
cookies (Privacy Policy)
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
Question Mark (?): This is used to match any one character from the string. For
example, if we have a string “CAT,” and the pattern is “C?T,” then VBA LIKE operator
returns TRUE. If the string is “CATCH and the patterns are “C?T,” then VBA LIKE
operator returns FALSE.
Asterisk (*): This matches zero or more characters. For example, if the string is
“Good,” and the pattern is “G**d,” VBA LIKE operator returns TRUE.
Brackets ([]): This matches any one single character specified in the brackets.
[Char-Char]: This matches any single character in the range Char-Char.
[!Chars]: This matches any single character not in the list.
[!Char-Char]: This matches any single character not in the range Char-Char.
You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to
For eg:
You can download this VBA Like Excel Template here – VBA Like Excel Template
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
Code:
Sub QuestionMark_Example1()
Dim k As String
k = "Good"
End Sub
In the above code, we have supplied the string as “Good,” and the pattern is “Go?d.”
Since the question mark can match a single character, it will show the result as “Yes.”
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
Code:
Sub QuestionMark_Example1()
Dim k As String
k = "Good Morning"
End Sub
In this case, it will show “No” because we have added one more word to the string, i.e.,
Morning. To match any number of characters, we need to use the asterisk.
Code:
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
Sub QuestionMark_Example2()
Dim k As String
k = "Good Morning"
End Sub
In the above example, I have added two asterisks before and after the character
“*Good*.” This will match the word “Good” in the string “Good Morning” and returns
“Yes.”
Code:
Sub QuestionMark_Example3()
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
Dim k As String
k = "Good Morning"
End Sub
The above code matches the single letter mentioned in the bracket “M” and returns
the result as Yes.
Code:
Sub QuestionMark_Example4()
Dim k As String
k = "Good Morning"
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
End Sub
This will return “No” because there are no characters from A to D in the string “Good
Morning.”
Code:
Sub QuestionMark_Example4()
Dim k As String
k = "Good Morning"
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub
This will return “Yes” because from A to H, we have a character “G” in the string “Good
Morning.”
Like this, we can use VBA “LIKE” operator to match any string from the pattern with
wild card characters.
Recommended Articles
This has been a guide to VBA LIKE. Here we will take through how to use VBA LIKE
operator using a question mark, Asterisk, and Brackets & Alphabets along with
examples and download an excel template. You may also have a look at other articles
related to Excel VBA –
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]
VBA Like Operator (Easy Examples) | How to Use "Like" in Excel VBA?
12 Hands-on Projects
43+ Hours
Certificate of Completion
https://www.wallstreetmojo.com/vba-like/[29/09/2021 03:14:25]