Table of Contents

RDC11017 — Like pattern MS-VBA matches otherwise than specified

Severity: Warning  ·  Family: RDC (Rubberduck Core analyzers)  ·  Raised by: LikePatternDepartureAnalyzer

The pattern of a Like operation is one that VBA matches differently from what the language specification says. The code works the way it does in Excel, Access or any other VBA host; this warning tells you that the specification describes something else, so another implementation of VBA could give a different answer.

There are two such patterns.

A [] or a [!]

The specification makes both a list of no character. In VBA:

  • [] matches no character at all: "a" Like "a[]" is True.
  • [!] matches the character !: "!" Like "[!]" is True.

An invalid pattern

A list with no closing ], such as "x[", or a range whose first character is above its last, such as [z-a], makes the pattern invalid. The specification says run-time error 93 (Invalid pattern string) is raised as soon as the match reaches it. VBA raises it only when the invalid part is tested against a character:

  • "x" Like "y[" is False: x does not match y, and the list is never tested.
  • "a" Like "[az-a]" is True: a matches the first item of the list, and the reversed range after it is never tested.
  • "a" Like "a*[z-a]" is False: no text is left when the * is reached.
  • "ab" Like "a*[z-a]" raises error 93.

Whether a range is reversed depends on Option Compare: under Option Compare Text a range is compared in lowercase, so [a-Z] is valid and [Z-a] is not.

How it is raised

Situation Example Resolution
The pattern has a [], meant to match nothing. If code Like "AB[]" Then Remove the []: it matches nothing in VBA.
The pattern has a [!], meant to match a !. If s Like "Hello[!]" Then Write the ! itself: "Hello!".
The pattern has a list with no closing ]. If s Like "x[" Then Close the list, or write [[] to match a [.
The pattern has a reversed range. If c Like "[z-a]" Then Write the range in order: [a-z].

Only a pattern written in the code as a String literal is reported, because only such a pattern is the same every time the code runs.

See also


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca