The Excel VBA Like operator lets you test whether a string matches a specified pattern—such as wildcards or character sets—returning True or False. It's built into VBA and requires no extra setup beyond having Excel installed with VBA enabled. This is especially useful for validating data, filtering records, or triggering logic based on partial text matches.

What You'll Need

  • Microsoft Excel (any version from 2010 onward, including Microsoft 365)
  • VBA enabled in Excel (the Developer tab must be visible)
  • Basic familiarity with writing or editing VBA macros

Step-by-Step

  1. Open Excel and press Alt + F11 (Windows) or Fn + Option + F11 (Mac) to open the Visual Basic for Applications (VBA) editor.
  2. In the VBA editor, go to Insert > Module to create a new standard module for your code.
  3. Type or paste a subroutine name, such as Sub TestLikeOperator(), and press Enter to create the procedure structure.
  4. Inside the subroutine, declare a string variable to test:
    Dim testString As String
  5. Assign a value to the variable—for example: testString = "AB123XYZ"
  6. Use the Like operator in an If statement with a pattern. For instance, to check if the string starts with "AB", followed by two digits, and ends with "XYZ":
    If testString Like "AB##XYZ" Then
  7. Inside the If block, add an action—such as displaying a message:
    MsgBox "Pattern matched!"
  8. Complete the block with End If and close the subroutine with End Sub.
  9. Press F5 (Windows) or Fn + F5 (Mac) to run the macro, or return to Excel and run it via Alt + F8.
  10. To test other patterns, modify the string variable and the Like pattern—e.g., try "A?C*" for strings starting with "A", any single character, then "C", then anything.

Common Mistakes

  • Confusing Like with = or InStr: The Like operator uses pattern syntax (e.g., # for digits), not literal comparison. Using Like "123" won’t match "0123" unless the pattern includes wildcards.
  • Ignoring case sensitivity: By default, Like is case-sensitive unless you include Option Compare Text at the top of the module. Without it, "abc" Like "ABC" returns False.
  • Misusing wildcards: The asterisk (*) matches zero or more characters—but only if placed at the start, middle, or end of the pattern. Writing "*A*B*" won’t match "ACB" because * doesn’t skip characters in the middle of a literal sequence.
  • Forgetting to declare variables: Using undeclared or variant-typed variables can cause runtime errors if the variable holds a non-string value (Like only works on strings).

Tips

  • Use Option Compare Text at the top of your module to make all string comparisons—including Like—case-insensitive across that module.
  • For complex validation (e.g., email formats), combine Like with Len() to enforce minimum/maximum lengths: e.g., str Like "*@*.*" And Len(str) >= 5.
  • Escape special characters by wrapping them in brackets: "*" becomes [*], ? becomes [?], and # becomes [#].
  • Use Like in loops to filter ranges: For Each cell In Range("A1:A100"): If cell.Value Like "Report*" Then ...
  • Store frequently used patterns in constants: Const DATE_PATTERN As String = "##/##/####" for date-like strings.

Troubleshooting

If your Like operator doesn’t behave as expected, follow these steps:

  1. Check variable type: Ensure the tested variable is a String. Use VarType(testString) = 8 to confirm.
  2. Verify pattern syntax: Confirm wildcards are used correctly: * (any chars), ? (one char), # (one digit), [charlist] (any one character in the list), [!charlist] (not in the list).
  3. Inspect case: Add Debug.Print testString Like "PATTERN" to the Immediate Window (Ctrl + G in VBA editor) to see the raw True/False result.
  4. Add Option Compare: If case might be the issue, add Option Compare Text to the top of the module and rerun.
  5. Test with a simple pattern: Replace your complex pattern with something basic like "A*" to confirm the operator itself works.
  6. Check for leading/trailing spaces: Use Trim(testString) to remove hidden whitespace that may break the match.

Frequently Asked Questions

Can the Like operator match multiple non-consecutive patterns?

No. Like evaluates one pattern at a time. To check if a string matches either "ABC" or "XYZ", use If str Like "ABC" Or str Like "XYZ" Then.

Does Like work with numbers stored as text?

Yes—as long as the variable is a string. Excel treats numbers stored as text like any other string, so "123" Like "##3" returns True.

How do I match a literal asterisk or question mark?

Enclose them in square brackets: "File[?].txt" matches "File?.txt" exactly. Similarly, "Price[$]" matches "Price$".

Can I use Like with cell ranges directly?

No—you must loop through each cell individually. Example: For Each c In Range("A1:A10"): If c.Value Like "*error*" Then c.Interior.Color = vbYellow.

Is Like faster than Regular Expressions (RegExp)?

For simple patterns, Like is usually faster and requires no extra references. For advanced pattern matching (e.g., repeating groups, lookaheads), use the Microsoft VBScript Regular Expressions library—but that requires setting a reference in the VBA editor (Tools > References).

Why does "ABCD" Like "A?CD" return False?

Because ? matches exactly one character. "A?CD" expects a 4-character string: A, one char, C, D. "ABCD" fits, so this should return True—if it doesn’t, check for hidden characters or enable Option Compare Text if case is involved.