Runtime Error 93: “Invalid pattern string,” explained and fixed

Error 93, “Invalid pattern string”, means the pattern handed to the Like operator cannot be parsed: an opening bracket with no closing bracket, a range like [z-a] whose ends are reversed, or a stray character class mistake. Fixed patterns written by the programmer fail at first test; the interesting cases are patterns built from user input or data, where a bracket typed into a search box becomes a broken pattern at run time. Either way it is a code bug: the pattern was never validated.

For people who just run the program

  1. Check what you typed in the search or filter box. If the tool matches with patterns, a bracket character in your search text is the most likely trigger. Simplify the text and try again.
  2. Remove special characters one at a time. Brackets, and combinations like [!], are the usual culprits; ordinary letters and digits never produce this error.
  3. Tell the maintainer the exact text you entered. Reproducing error 93 requires the precise pattern; “some brackets” is not enough to recreate it.
  4. Do not assume the document is at fault. The pattern comes from the input side, not from the data being searched.

For people who write the code

  1. Escape user text before embedding it in a pattern. In Like patterns, literal brackets must themselves be written as [[] and []]. A sanitizer that wraps each special character in brackets makes arbitrary text safe:
  2. Function EscapeLike(s As String) As String
        s = Replace(s, "[", "[[]")
        s = Replace(s, "]", "[]]")
        EscapeLike = s
    End Function
    If fileName Like "*" & EscapeLike(userText) & "*" Then ...
  3. Validate ranges when building patterns programmatically. In a character range the start must sort before the end: [a-z] is legal, [z-a] raises error 39's cousin, error 93. Sort generated range endpoints.
  4. Decide whether Like is even the right tool. If the input is meant as plain text, compare with InStr instead of Like. Patterns are a feature to offer deliberately, not a default that turns every search box into a pattern parser.
Patterns assembled from configuration files fail in production, not in testing. Developers test with their own tidy patterns; the broken one arrives later in a config value or a user's saved filter. Validate any pattern at load time with a trial comparison against an empty string so error 93 surfaces early and with context.

If it still happens

If the pattern looks visibly correct, check for invisible characters: a non-breaking space or a smart quote copied from a document into the pattern source changes its meaning and can make a legal-looking pattern unparseable. Print the pattern's character codes to see what is really there.

Common questions

Which characters are special in a Like pattern? Question mark matches one character, asterisk matches any run, hash matches one digit, and brackets define character lists and ranges. A bracket that opens a list must be closed, or the pattern is invalid.

Is Like the same as regular expressions? No. Like is Visual Basic's older, simpler pattern syntax with its own rules. Code that feeds a regular expression into Like (or the reverse) produces invalid patterns and mismatched results.

Related errors

Runtime Error 5 · Runtime Error 380 · Runtime Error 13