Identifying Values that Don't Follow a Specific Pattern

by Allen Wyatt
(last updated September 16, 2017)

2

Vishwajeet has a series of part numbers in a column. These part numbers must follow a specifc pattern (2 numbers, 5 letters, 4 numbers, 1 letter, 1 number, 1 letter, and 1 number). He wonders if there is a way to easily identify which of the cells in the column vary from this pattern.

There are a number of ways you can approach this task, depending on the true nature of your data. For instance, you could use a formula like this in a helper column:

=AND(LEN(A1)=15,ISNUMBER(--LEFT(A1,2)),ISTEXT(MID(A1,3,5)),
ISNUMBER(--MID(A1,8,4)),ISTEXT(MID(A1,12,1)),ISNUMBER(--
MID(A1,13,1)),ISTEXT(MID(A1,14,1)),ISNUMBER(--RIGHT(A1,1)))

The formula (which is quite long) returns True or False, depending on whether the pattern is correct or not. There is a problem with the formula, however. It won't catch symbols used in place of letters (such as a dollar sign or an asterisk) and it won't catch some symbols used in place of numbers (such as a period or a percent sign). The reason is that the ISTEXT function considers symbols to be text and the ISNUMBER function parses something like "1.23" as a number.

If you want to catch this improper use of symbols, the following formula could be used:

=AND(LEN(A1)=15,ISNUMBER(SUM(SEARCH(MID(A1,{1,2,8,9,10,11,
13,15},1),"0123456789"),SEARCH(MID(A1,{3,4,5,6,7,12,14},1),
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))))

This one works because it uses the SEARCH function to effectively check every character in the part number. One would think that you might need to enter the formula as an array formula (terminating it with Ctrl+Shift+Enter), but interestingly enough, I get no difference in results when I use it as a regular formula vs. an array formula.

If you need to check the part number patterns quite a bit, you may want to consider using a macro to do the checking. The following is a short user-defined function that uses the Like operator to see if the pattern is followed.

Function CheckPattern(rCell As Range) As Boolean
    Dim sPattern As String

    sPattern = "##[A-Z][A-Z][A-Z][A-Z][A-Z]####[A-Z]#[A-Z]#"

    CheckPattern = rCell.Value Like sPattern
End Function

Note the use of the sPattern variable. This is the pattern to be followed when the Like operator does its comparison. Each occurrence of the # symbol means that any digit can be in this position. Each occurrence of [A-Z] means that the position can be one letter in the range of A to Z.

You can find more about the meaning of the characters you can include in the pattern by visiting this page at one of Microsoft's sites:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator

This site is actually for Visual Basic, not VBA, but this particular page of information will work just fine in VBA.

In order to use the CheckPattern user-defined function, you can place the following in any cell in your worksheet:

=CheckPattern(A1)

This assumes that the part number is in cell A1, as do all the other formulas presented in this tip.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3391) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Resetting Excel Menus

Excel allows you to easily change what appears on its menus. If you later want to return to Excel's default menu structure, ...

Discover More

Customizing File Explorer's Display

File Explorer is a powerful utility to use for navigating among your files and folders. And it can be customized a great deal ...

Discover More

Macros On the Quick Access Toolbar

When you configure Word to your needs, a common thing to do is to add commands and macros to the Quick Access Toolbar. When ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Rounding to the Nearest Half Dollar

When working with financial data, it's easy to round values to the nearest dollar. What if you want them rounded to the ...

Discover More

Deriving Monthly Median Values

When processing huge amounts of data, it can be a challenge to figure out how to derive the aggregate values you need. This ...

Discover More

Formatting Canadian Postal Codes

Postal codes in Canada consist of six characters, separated into two groups. This tip explains the format and then shows how ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 0?

2017-09-20 12:35:10

Willy Vanhaelen

The need of a helper column can be avoided by using conditional formatting combined with the User Defined Function (UDF) of this tip. You will immediatly see which part numbers have a wrong pattern with the additiional advantage that when entering a new part numbers with a wrong pattern you will notice it at once.
1) copy the UDF of this tip to a module
2) with column A highlighted, click on Conditional Formatting in the Home tab (Styles section)
3) select New Rule...
4) Excel displays the New Formatting Rule dialog box.
5) select "Use a formula to determine which cells to format"
6) the dialog changes (see figure below)
7) type or copy/past the following formula: =AND(CheckPattern(A1)=FALSE,A1<>"")

(see Figure 1 below)

8) Select the formatting of your liking (red background in the image above)
9) click OK

Here is a oneliner version of the UDF:
Function CheckPattern(rCell As Range) As Boolean
CheckPattern = rCell.Value Like "##[A-Z][A-Z][A-Z][A-Z][A-Z]####[A-Z]#[A-Z]#"
End Function

Figure 1. 


2017-09-17 00:43:04

Nir Liberman

I`m not sure that the first formula is correct:

cell A1='1A'
formula: =ISTEXT(MID(A1,1,1))
result: TRUE !!!!

and the complete formula:
111aaaa1111a1a1 = TRUE (false true)
11aaaaa1111a1a1 = TRUE (true true)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.