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

  1. Open the Visual Basic Editor – In Excel, press Alt+F11. If you don't see the Project Explorer on the left, go to View > Project Explorer.
  2. Insert a module – In the Project Explorer, right-click any workbook name, choose Insert, then Module. A blank code window opens.
  3. Write a test subroutine – Type Sub TestLike() and press Enter. VBA adds End Sub automatically.
  4. 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). Press F5 to run, then open the Immediate Window (Ctrl+G) to see True.
  5. Test a question mark wildcard – Add Debug.Print "cat" Like "c?t". The ? matches exactly one character, so this returns True. Change "cat" to "cart" and it returns False.
  6. 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 returns True. Try "d1" to see False.
  7. Use a negated character list – Write Debug.Print "d1" Like "[!a-c]1". The exclamation mark negates the list, so this returns True because "d" is not in a-c.
  8. 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 returns True. Change a digit to a letter to see False.
  9. 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.
  10. 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.
  11. Use Like with Option Compare – At the top of the module, type Option Compare Text to make comparisons case-insensitive. Without it, VBA uses binary comparison (case-sensitive). For example, "abc" Like "A*" returns False by default, but True with Option Compare Text.
  12. Test a pattern with multiple wildcards – Write Debug.Print "a-b-c" Like "a*-*". This returns True because it starts with "a" and has a hyphen after some characters.
  13. Escape special characters – If you need to match a literal asterisk, question mark, or bracket, enclose it in brackets. For example, "100%" Like "100[%]" returns True. Without brackets, % is a normal character, but * is a wildcard.
  14. 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 Like is case-sensitive by default. Use Option Compare Text at 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 use Like.
  • 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 Compare at 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 Like with Select Case for 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 Like with Len to validate exact lengths. For a 5-digit ZIP code, use s Like "#####" and Len(s) = 5.
  • Remember that Like works on strings only. If you have a number, convert it with CStr first, or use Str.
  • For performance, Like is faster than multiple InStr calls in a loop, especially with large ranges.
  • Use Option Compare Text at the top of the module to make all string comparisons case-insensitive, not just Like.

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.