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.
[!], are the usual culprits; ordinary letters and digits never produce this error.[[] and []]. A sanitizer that wraps each special character in brackets makes arbitrary text safe:Function EscapeLike(s As String) As String
s = Replace(s, "[", "[[]")
s = Replace(s, "]", "[]]")
EscapeLike = s
End Function
If fileName Like "*" & EscapeLike(userText) & "*" Then ...
[a-z] is legal, [z-a] raises error 39's cousin, error 93. Sort generated range endpoints.InStr instead of Like. Patterns are a feature to offer deliberately, not a default that turns every search box into a pattern parser.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.
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.