Does a string match a pattern

The IsLike function is very simple (but also very useful). This function returns true if a text string matches a specified pattern.

This function, which follows, is remarkably simple. As you can see, the function is essentially a wrapper that lets you take advantage of VBA's powerful Like operator in your formulas.

Function IsLike(text As String, pattern As String) As Boolean ' Returns true if the first argument is like the second

IsLike = text Like pattern End Function

This IsLike function takes two arguments:

■ text : A text string or a reference to a cell that contains a text string

■ pattern : A string that contains wildcard characters according to the following list:

; Open table as spreadsheet

Any single character

Zero or more characters

Any single digit (0-9)

Any single character in charlist

Character(s) in pattern

Matches in text

The following formula returns true because * matches any number of characters. It returns true if the first argument is any text that begins with g.

The following formula returns true because ? matches any single character. If the first argument were "Unit12" , the function would return false .

=IsLike("Unit1","Unit?")

The next formula returns true because the first argument is a single character in the second argument.

The following formula returns true if cell A1 contains a, e, i, o, u, A, E, I, O, or U. Using the UPPER function for the arguments makes the formula not case-sensitive.

The following formula returns TRUE if cell A1 contains a value that begins with 1 and has exactly three digits (that is, any integer between 100 and 199).

0 0

Responses

  • smeralda
    How to match with single digit value in vba?
    7 years ago

Post a comment