Conditionally Formatting for a Pattern

by Allen Wyatt
(last updated May 9, 2015)

5

Kim has a column of data used to contain a location code. This code consists of a single letter followed by two digits, such as A03 or B12. Kim would like to conditionally format the column so that anything entered into the column that doesn't use this pattern is highlighted in some way.

There are many ways that this problem can be approached. Each approach depends on developing a formula that can be used within a conditional formatting rule to return either True or False and trigger the conditional format. (This tip won't go into how to create a conditional formatting rule, but will instead focus on the various formulas that can be used in the rule. How you create conditional formatting rules is covered in other ExcelTips.)

Whatever formula is put together has to test three things:

  • There are exactly three characters in the string.
  • The first character is a letter.
  • The second and third characters are digits.

Finding out if the text in a cell has only three characters is rather easy; you can use the LEN function to do it:

=LEN(A1)=3

Finding out if the first character is a letter is also rather easy. In fact there are a couple of ways it can be done. Any of the following will return True if the first character is a letter:

=AND(CODE(LEFT(A1,1))>64,CODE(LEFT(A1,1))<91)
=AND(LEFT(A1,1)>="A",LEFT(A1,1)<="Z")

These check to make sure that only an uppercase letter is in the first position. If you want to also accept lowercase letters, then you can use a variation of the second test:

=AND(UPPER(LEFT(A1,1))>="A",UPPER(LEFT(A1,1))<="Z")

If both upper- and lowercase letters are acceptable (along with virtually any other symbol), then you might consider using the following test:

=NOT(ISNUMBER(LEFT(A1,1)+0))

Here are a couple of ways you can apply the third test-whether the second and third characters are digits:

=ISNUMBER(VALUE(RIGHT(A1,2)))
=ISNUMBER(--RIGHT(A1,2))

Note that these approaches treat the last two characters together. This means that "1", "11," and "111" would all pass the test—they successfully check out as numbers. If your formula were checking only the last two digits, this could be a problem, but the fact that you will also include the first check (for the overall length of the string in the cell and that it must be 3), then it doesn't present a problem at all.

The trick, now, is to combine your approach-of-choice for each of the three tests into a single formula. This can be done using the AND function. I'll just pick the shortest from each of the tests and combine them in this way:

=AND(LEN(A1)=3, AND(LEFT(A1,1)>="A",LEFT(A1,1)<="Z"), =ISNUMBER(--RIGHT(A1,2)))

As written, this formula returns True if all the tests are passed, which means that the cell contains a location code with a valid pattern. This would work great as a conditional format if Kim were to format the column as a color (say, green) and then use the conditional format to remove the green color. This may seem backward, and you may actually only want to apply a format if the pattern isn't met. If that is the case, then simply encase the formula in a NOT function to reverse the True/False that is returned:

=NOT(AND(LEN(A1)=3, AND(LEFT(A1,1)>="A",LEFT(A1,1)<="Z"), =ISNUMBER(--RIGHT(A1,2))))

As you can tell, using a formula like this can be a bit tricky. You could, if you prefer, created a UDF (user-defined function) that would make the conditional formatting rule a bit shorter. The following macro is a good way to go:

Function IsBadPattern(sCell As String) As Boolean
    IsBadPattern = Not(sCell Like "[A-Z][0-9][0-9]")
End Function

To use the UDF in your conditional formatting rule, all you need to do is use the following formula:

=IsBadPattern(A1)

The result of the UDF will be TRUE if the string in the referenced cell doesn't match the pattern you wanted. As written, it won't allow for the use of lowercase letters in the first character position. If you need to allow lowercase letters, you don't need to change the UDF. Instead, change the formula to the following:

=IsBadPattern(UPPER(A1))

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

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

Weird Mouse Shortcut

If you like to use the mouse in your worksheet navigation efforts, you'll want to pay attention to this tip. Here you ...

Discover More

Upside-Down Printing

Putting words on the printed page is easy in Word. Rotating those words in different manners can be a bit trickier. This ...

Discover More

Calculating Months of Tenure

Need to know the number of months between two dates? It's easy to figure out if you use the DATEDIF function.

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Conditionally Formatting Non-Integers

The conditional formatting capabilities of Excel are very helpful when you want to call attention to different values ...

Discover More

Shading Based on Odds and Evens

You can use conditional formatting to add shading to various cells in your worksheet. This tip shows how you can shade ...

Discover More

Highlighting Cells Containing Specific Text

If you want to highlight cells that contain certain characters, you can use the conditional formatting features of Excel ...

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 nine minus 4?

2015-05-11 07:36:37

Mark

There's a typo in two of the formulas provided. The equal-sign before ISNUMBER should be removed - for example,
=AND(LEN(A1)=3, AND(LEFT(A1,1)>="A",LEFT(A1,1)<="Z"), ISNUMBER(--RIGHT(A1,2)))


2015-05-09 15:47:55

Jurgen

Hi,

using a UDF obviously has a very distinct disadvantage: it requires the user to allow macros/scripting.


2015-05-09 13:44:01

John Hamm

It is not necessary to specify the leftmost character when applying the CODE function. CODE("hello") yields the same result as CODE("h").

The second and third characters should be tested independently. When they are tested together, entries such as B-4, B+4 and B.4 pass as conforming to the pattern.

I prefer applying a custom format to only those values that do not match the pattern. Here is a formula that is TRUE when either of the second two characters is not a digit: =ISERROR(MID(A1,2,1)+RIGHT(A1,1))


2015-05-09 13:07:23

Willy Vanhaelen

Instead of combining the 3 tests you can enter them separatly as 3 consecutive rules.

=LEN(A1)<>3
=NOT(ISNUMBER(--RIGHT(A1,2)))
=AND(LEFT(A1,1)>="A",LEFT(A1,1)<="Z")

Leave the first two with "No Format Set" and check "Stop If True".
For the third rule set the format of your choice.

The third formula is not case sensitive so UPPER() is not needed and
=AND(LEFT(A1,1)>="A",LEFT(A1,1)<="Z")
=AND(LEFT(A1,1)>="a",LEFT(A1,1)<="z")
give the same result.


2015-05-09 07:51:42

Mohammed Hasil

Wow! Nice ideas!!! Thanks.


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.