The Like operator in Excel VBA lets you compare a string against a pattern, returning True if the string matches and False if it doesn't. It's a compact replacement for multiple InStr checks or If statements when you need to validate formats, filter data, or find partial matches. You can use it in any VBA macro or function. Before you start, you need the Visual Basic Editor open (press Alt+F11 in Excel) and a module to write code in—insert one from the menu if you don't have it. This guide uses the VBA editor's standard menus; exact labels may differ slightly by Excel version.
What You'll Need
- Excel with VBA enabled (any version that supports macros).
- A module in the Visual Basic Editor (Insert > Module).
- Basic familiarity with writing and running a macro (F5 to run).
Step-by-Step
- Open the Visual Basic Editor – In Excel, press
Alt+F11. If you don't see the Project Explorer on the left, go toView > Project Explorer. - Insert a module – In the Project Explorer, right-click any workbook name, choose
Insert, thenModule. A blank code window opens. - Write a test subroutine – Type
Sub TestLike()and press Enter. VBA addsEnd Subautomatically. - Use the Like operator with a simple pattern – Inside the subroutine, type:
Debug.Print "Hello" Like "H*"
This checks if the string starts with "H". The asterisk*matches any number of characters (including zero). PressF5to run, then open the Immediate Window (Ctrl+G) to seeTrue. - Test a question mark wildcard – Add
Debug.Print "cat" Like "c?t". The?matches exactly one character, so this returnsTrue. Change "cat" to "cart" and it returnsFalse. - Use character lists – Write
Debug.Print "a1" Like "[a-c]1". The brackets[a-c]match any single character in the range a to c. This returnsTrue. Try "d1" to seeFalse. - Use a negated character list – Write
Debug.Print "d1" Like "[!a-c]1". The exclamation mark negates the list, so this returnsTruebecause "d" is not in a-c. - Combine patterns for a real check – For a phone number format like (123) 456-7890, use:
Debug.Print "(123) 456-7890" Like "(###) ###-####"
Here#matches any single digit. This returnsTrue. Change a digit to a letter to seeFalse. - Use Like in an If statement – Write:
Dim name As String: name = "John Smith"If name Like "John *" Then Debug.Print "First name is John"
This checks if the name starts with "John " and prints a message. - Loop through a range with Like – Write code to filter cells:
Dim cell As Range
For Each cell In Range("A1:A10")
If cell.Value Like "*@*" Then
cell.Offset(0, 1).Value = "Has @"
End If
Next cell
This checks each cell in A1:A10 for an @ symbol and writes a note in column B. - Use Like with Option Compare – At the top of the module, type
Option Compare Textto make comparisons case-insensitive. Without it, VBA uses binary comparison (case-sensitive). For example,"abc" Like "A*"returnsFalseby default, butTruewithOption Compare Text. - Test a pattern with multiple wildcards – Write
Debug.Print "a-b-c" Like "a*-*". This returnsTruebecause it starts with "a" and has a hyphen after some characters. - Escape special characters – If you need to match a literal asterisk, question mark, or bracket, enclose it in brackets. For example,
"100%" Like "100[%]"returnsTrue. Without brackets,%is a normal character, but*is a wildcard. - Use Like in a function – Create a custom function:
Function IsEmail(s As String) As Boolean
IsEmail = s Like "*@*.*"
End Function
Call it from a cell like=IsEmail(A1)to check for a basic email pattern.
Common Mistakes
- Forgetting that
Likeis case-sensitive by default. UseOption Compare Textat the top of the module to change that. - Using a single equals sign for comparison.
If s = "*"checks for a literal asterisk, not a wildcard. You must useLike. - Not escaping special characters. To match a literal question mark, use
"?"in brackets, not just?. - Using
#in a pattern expecting it to match any character.#matches only digits, not letters or symbols. - Mixing up
?and*.?matches exactly one character;*matches zero or more. Using*when you need exactly one character will match too much. - Writing patterns that are too broad. For example,
"*"matches any string, including empty strings. That might be fine, but if you want at least one character, use"?*". - Forgetting to declare
Option Compareat the top of the module—it only affects modules where it appears.
Tips
- Test patterns in the Immediate Window (
Ctrl+G) before putting them in code. Type? "abc" Like "a*"and press Enter to see the result. - Use
LikewithSelect Casefor clean multi-condition checks. For example:Select Case True
Case name Like "Dr.*" : title = "Doctor"
Case name Like "Mr.*" : title = "Mister"
Case Else : title = "Other"
End Select - Combine
LikewithLento validate exact lengths. For a 5-digit ZIP code, uses Like "#####"andLen(s) = 5. - Remember that
Likeworks on strings only. If you have a number, convert it withCStrfirst, or useStr. - For performance,
Likeis faster than multipleInStrcalls in a loop, especially with large ranges. - Use
Option Compare Textat the top of the module to make all string comparisons case-insensitive, not justLike.
Troubleshooting
Like returns False when you expect True. Check case sensitivity first. If your pattern has uppercase letters and the string has lowercase, it fails without Option Compare Text. Also verify the pattern: "abc" Like "a*" is True, but "abc" Like "a" is False because the pattern must match the entire string.
Pattern matches too much. If "*" matches everything, narrow it down. Use "?*" to require at least one character, or use character lists like "[A-Z]*" to restrict the first character.
Getting a compile error. Make sure you're not using Like as a variable name. Also check that the pattern is in quotes. Like expects a string expression on the right.
Nothing prints in the Immediate Window. Ensure you pressed Ctrl+G to open it, and that your code actually runs. If you're in a function, call it from a sub or use Debug.Print inside the function.
Like doesn't work with numbers. VBA converts numbers to strings automatically in most cases, but to be safe, use CStr(number) before comparing.
Option Compare Text doesn't affect my module. It must be the first line in the module, before any procedures. If you put it after a Sub, it won't work.
Frequently Asked Questions
Can I use Like to check if a string contains a substring?
Yes, use "*substring*" as the pattern. For example, "Hello world" Like "*world*" returns True.
Is Like case-sensitive?
By default, yes. Add Option Compare Text at the top of the module to make it case-insensitive.
What does the # wildcard match?
It matches exactly one digit (0-9). Use it for numeric patterns like phone numbers or ZIP codes.
How do I match a literal asterisk?
Put it in brackets: "*" in a pattern means wildcard, but "[*]" matches a single asterisk character.
Can I use Like in a worksheet formula?
No, Like is VBA-only. In worksheet formulas, use COUNTIF with wildcards, or ISNUMBER(SEARCH(...)) for partial matches.
Does Like work with empty strings?
Yes. "" Like "*" returns True because * matches zero characters. To match only empty strings, use "" Like "" or Len(s) = 0.