Pages

Microsoft Office Excel 2007 Conditional Formatting and Data Validation

This chapter explores two very useful Excel features: conditional formatting and data validation. You may not think these features have much to do with formulas. As you'll see, though, when you toss formulas into the mix, conditional formatting and data validation can perform some amazing feats.
Conditional Formatting
Conditional formatting enables you to apply cell formatting selectively and automatically, based on the contents of the cells. For example, you can set things up such that all negative values in a range have a light yellow background color. When you enter or change a value in the range, Excel examines the value and evaluates the conditional formatting rules for the cell. If the value is negative, the background is shaded. If not, no formatting is applied.
New 
Conditional formatting has improved significantly in Excel 2007 and is now even more useful for visualizing numeric data. In some cases, you may be able to use conditional formatting in lieu of using a chart.

Conditional formatting is a useful way to quickly identifying erroneous cell entries or cells of a particular type. You can use a format (such as bright red cell shading) to make particular cells easy to identify.
  • Greater than 10: Values greater than 10 are highlighted with a different background color. This rule is just one of many numeric value related rules that you can apply.
  • Above average: Values that are higher than the average value are highlighted.
  • Duplicate values: Values that appear more than one time are highlighted.
  • Words that contain X: If the cell contains the letter X (upper- or lowercase), the cell is highlighted.
  • Data bars: Each cell displays a horizontal bar, proportional to its value.
  • Color Scale: The background color varies, depending on the value of the cells. You can choose from several different color scales or create your own.
  • Icon Set: This is one of many icon sets, which display a small graphic in the cell. The graphic varies, depending on the cell value.
  • Icon Set: This is another icon set.
  • Custom Rule: The rule for this checkerboard pattern is based on a formula:

=MOD(ROW(),2)=MOD(COLUMN(),2)
Specifying Conditional Formatting
To apply a conditional formatting rule to a cell or range, select the cells and then use one of the commands on the Home Styles Conditional Formatting drop-down to specify a rule. The choices are
  • Highlight Cell Rules: Examples rules include highlighting cells that are greater than a particular value, are between two values, contain specific text string, or are duplicated.
  • Top Bottom Rules: Examples include highlighting the top ten items, the items in the bottom 20 percent, or items that are above average.
  • Data Bars: This applies graphic bars directly in the cells, proportional to the cell's value.
  • Color Scales: This applies background color, proportional to the cell's value.
  • Icon Sets: This displays icons directly in the cells. The icons depend on the cell's value.
  • New Rule: This enables you to specify other conditional formatting rules, including rules based on a logical formula.
  • Clear Rules: This deletes all the conditional formatting rules from the selected cells.
  • Manage Rules: This displays the Conditional Formatting Rules Manager dialog box, in which you create new conditional formatting rules, edit rules, or delete rules.

Excel 2007 Improvements
If you've used conditional formatting in a previous version of Excel, you'll find lots of improvements in Excel 2007, and the feature is now much easier to use. Many conditional formatting rules that previously required a formula are now built in. Improvements include the following:
  • In the past, it was far too easy to accidentally wipe out conditional formatting by copying and pasting a range of cells to cells that contain conditional formatting. This problem has been corrected in Excel 2007.
  • Excel 2007 includes conditional formatting visualizations based on a range of data. These visualizations include data bars, color scales, and icon sets.
  • You're no longer limited to three conditional formatting rules per cell. In fact, you can specify any number of rules.
  • In the past, if more than one conditional formatting rule evaluated to true, only the first conditional format was applied. In Excel 2007, all the format rules are applied. For example, assume that you have a cell with two rules: One rule makes the cell's contents italic, and another rules makes the background color green. If both conditions are true, both formats are applied. When conflicts arise, (for example, red background versus green background), the first rule is used.
  • Excel 2007 allows number formatting to result from conditional formatting.
  • In previous versions, a conditional formatting formula could not reference cells in a different worksheet. Excel 2007 removes that restriction.

FORMATTING TYPES YOU CAN APPLY
When you select a conditional formatting rule, Excel displays a dialog box that's specific to that rule. These dialog boxes have one thing in a common: a drop-down list with common formatting suggestions. shows the dialog box that appears when you choose Home Styles Conditional Formatting Highlight Cells Rules Between. This particular rule applies the formatting if the value in the cell falls between two specified values. In this case, you enter the two values (or enter cell references) and then use the drop-down control to choose the type of formatting to display if the condition is met.

The formatting suggestions in the drop-down control are just a few of thousands of different formatting combinations. In most cases, none of Excel's suggestions are what you want, so you choose the Custom Format option to display the Format Cells dialog box. You can specify the format in any or all of the four tabs: Number, Font, Border, and Fill.

Note 
The Format Cells dialog box used for conditional formatting is a modified version of the standard Format Cells dialog box. It doesn't have the Number, Alignment, and Protection tabs; and, some of the Font formatting options are disabled. The dialog box also includes a Clear button that clears any formatting already selected.

MAKING YOUR OWN RULES
For do-it-yourself types, Excel provides the New Formatting Rule dialog box. Access this dialog box by choosing Home Styles Conditional Formatting New Rules.

The New Formatting Rule dialog box lets you re-create all the conditional format rules available via the Ribbon as well as create new rules.

First, select a general rule type from the list at the top of the dialog box. The bottom part of the dialog box varies, depending on your selection at the top. After you specify the rule, click the Format button to specify the type of formatting to apply if the condition is met. An exception is the first rule type, which doesn't have a Format button. (It uses graphics rather than cell formatting.)
Following is a summary of the rule types:
  • Format All Cells Based on Their Values: Use this rule type to create rules that display data bars, color scales, or icon sets.
  • Format Only Cells That Contain: Use this rule type to create rules that format cells based on mathematical comparisons (greater than, less than, greater than or equal to, less than or equal to, equal to, not equal to, between, or not between). You can also create rules based on text, dates, blanks, nonblanks, and errors. This rule type is very similar to how conditional formatting was set up in previous versions of Excel.
  • Format Only Top or Bottom Ranked Values: Use this rule type to create rules that involve identifying cells in the top n, top n percent, bottom n, and bottom n percent.
  • Format Only Values That Are Above or Below Average: Use this rule type to create rules that identify cells that are above average, below average, or within a specified standard deviation from the average.
  • Format Only Unique or Duplicate Values: Use this rule type to create rules that format unique or duplicate values in a range.
  • Use a Formula to Determine Which Cells to Format: Use this rule type to create rules based on a logical formula. See "Creating Formula-Based Rules," later in this chapter.

Conditional Formats That Use Graphics
This section describes the three conditional formatting options that are new to Excel 2007: data bars, color scales, and icon sets. These types of conditional formatting can be useful for visualizing the values in a range.

USING DATA BARS
The data bars conditional format displays horizontal bars directly in the cell. Length of the bar is based on the value of the cell, relative to the other values in the range.
shows a simple example of data bars. It's a list of customers and sales amounts. I applied data bar conditional formatting to the values in column B. You can tell at a glance where the higher values are.
On the CD 
The examples in the section are available on the companion CD-ROM. The workbook is named  data bars examples.xlsx.


Tip 
The differences between the bar lengths become more prominent when you increase the column width.

Excel provides quick access to six data bar colors via the Home Styles Conditional Formatting Data Bars command. For additional choices, click the More Rules option, which displays the New Formatting Rule dialog box. Use this dialog box to
  • Show the bar only (hide the numbers).
  • Adjust how the bars relate to the values (use the Type and Value controls).
  • Change the color of the bars.

Note 
Data bars are always displayed as a color gradient (from dark to light), and you can't change the display style. Also, the colors used are not theme colors. If you apply a new document theme, the data bar colors do not change.

If you make adjustments in this dialog box, you can use the Preview button to see the formats before you commit to them by clicking OK.
Note 
You may notice something odd about the data bars in Contrary to what you may expect, a cell with a zero value displays a data bar. Data bar conditional formatting always displays a bar for every cell, even for zero values. The smallest value in the range always has a bar length equal to ten percent of the cell's width. Unfortunately, Excel provides no direct way to modify the minimum percent setting. However, if you're familiar with VBA, you can use a statement like the following to set the minimum display width for a range that uses conditional formatting data bars:
Range("B2:B123").FormatConditions(1).PercentMin = 1
After executing this statement, the minimum value in the range will display a bar length equal to one percent of the cell's width-and zero value cells will not display a data bar.

USING DATA BARS IN LIEU OF A CHART
Using the data bars conditional formatting can sometimes serve as a quick alternative to creating a chart. shows a three-column table of data, with data bars applied in the third column. The third column of the table contains references to the values in the second column. The conditional formatting in the third column uses the Show Bars Only option.
shows an actual bar chart created from the same data. The bar chart takes about the same amount of time to create and is a lot more flexible. But for a quick-and- dirty chart, data bars are a good option-especially when you need to create several such charts.

USING COLOR SCALES
The color scale conditional formatting option varies the background color of a cell based on the cell's value, relative to other cells in the range.
shows a range of cells that use color scale conditional formatting. It depicts the number of employees on each day of the year. This is a three-color scale that uses red for the lowest value, yellow for the midpoint, and green for the highest value. Values in between are displayed using a color within the gradient.

On the CD 
This workbook, named  daily staffing level.xlsx, is available on the companion CD-ROM.

Excel provides four two-color scale presets and four three-color scales presets, which you can apply to the selected range by choosing Home Styles Conditional Formatting Color Scales.
To customize the colors and other options, choose Home Styles Conditional Formatting Color Scales More Rules. This command displays the New Formatting Rule dialog box/
It's important to understand that color scale conditional formatting uses a gradient. For example, if you format a range with a two-color scale, you will get a lot more than two colors because you'll get colors with the gradient between the two specified colors.
shows an extreme example that uses color scale conditional formatting on a range of 10,000 cells (100 rows x 100 columns). The worksheet is zoomed down to 20% to display a very smooth three-color gradient. The range contains formulas like this one, in cell C5:
=SIN($A2)+COS(B$1)Values
in column A and row 1 range from 0 to 4.0, in increments of 0.04.

The result, when viewed on your, screen is stunning. (It loses a lot when converted to grayscale.)
On the CD 
This workbook, named  extreme color scale.xlsx, is available on the companion CD-ROM.



Note 
You can't hide the cell contents when using a color scale rule, so I formatted the cells using this custom number format:
;;;

USING ICON SETS
Yet another conditional formatting option is to display an icon in the cell. The icon displayed depends on the value of the cells.
To assign an icon set to a range, select the cells and choose Home Styles Conditional Formatting Icon Sets. Excel provides 17 icon sets to choose from. Note: You can't create your set of icons. The number of icons in the sets ranges from 3–5.
shows a simple example that uses the icon set named Three Symbols (Uncircled). The symbols graphically depict the status of each project, based on the value in column C.

On the CD 
All the icon set examples in this section are available on the companion CD-ROM. The workbook is named  icon set examples.xlsx.

By default, the symbols are assigned using percentiles. For a three-symbol set, the items are grouped into three percentiles. For a four-symbol set, they're grouped into four percentiles. And for a five-symbol set, the items are grouped into five percentiles.
If you would like more control over how the icons are assigned, choose Home Styles Conditional Formatting Icon Sets More Rules to display the New Formatting Rule dialog box. shows how to modify the icon set rules such that only projects that are 100% completed get check mark icons. Projects that are 0% completed get an X icon. All other projects get an exclamation point icon.
shows a table that contains two test scores for each student. The Change column contains a formula that calculates the difference between the two tests. The Trend column uses an icon set to display the trend graphically.

This example uses the icon set named 3 Arrows, and I customized the rule:
  • Up Arrow: When value is >=5
  • Level Arrow: When value <5 and >= -5
  • Down Arrow: When value is >=5
In other words, difference of five points or less in either direction is considered an even trend. An improvement of more than five points is considered a positive trend, and a decline of more than five points is considered a negative trend.

Note 
The Trend column contains a formula that references the Change column. I used the Show Icon Only option in the Trend column, which also centers the icon in the column.

In some cases, you may want to display only one icon from an icon set. Excel doesn't provide this option directly, but displaying a single icon is possible if you use two rules. shows a range of values. Only the values greater than or equal to 80 display an icon.

Here's how to set up an icon set such that only values greater than or equal to 80 display an icon:
  1. Select the cells, choose Home Styles Conditional Formatting Icon Sets, and select any icon set. Keep in mind that only the last icon of the set will be used.
  2. With the range selected, choose Home Styles Conditional Formatting Manage Rules. Excel displays its Conditional Formatting Rules Manager dialog box.
  3. Click Edit Rule to display the Edit Formatting Rule dialog box.
  4. Change the first icon setting to When Value Is >= 80 and specify Number as the Type. Leave the other icon settings as they are, and then click OK to return to the Conditional Formatting Rules Manager.
  5. Click New Rule and then choose this rule type: Format Only Cells That Contain.
  6. In the bottom section of the dialog box, specify Cell Value Less Than 80 and then click OK to return to the Conditional Formatting Rules Manager. The range now has two rules.
  7. Place a check mark next to Stop If True for the first rule. Click OK.

The first rule checks whether the value is less than 80. If so, rule checking stops, and no conditional formatting is applied. If the value is greater than or equal to 80, the second rule kicks in. This rule indicates that values greater than or equal to 80 are displayed with an icon.

Working with Conditional Formats
This section describes some additional information about conditional formatting that you may find useful.
MANAGING RULES
The Conditional Formatting Rules Manager dialog box is useful for checking, editing, deleting, and adding conditional formats. Access this dialog box by choosing Home Styles Conditional Formatting Manage Rules.
You can specify as many rules as you like by clicking the New Rule button. As you can see in, cells can even use data bars, color scales, and icon sets all at the same time-although I can't think of a good reason to do so.

COPYING CELLS THAT CONTAIN CONDITIONAL FORMATTING

Conditional formatting information is stored with a cell much like how standard formatting information is stored with a cell. As a result, when you copy a cell that contains conditional formatting, you also copy the conditional formatting.

Tip 
To copy only the formatting (including conditional formatting), use the Paste Special dialog box and select the Formats option.

Inserting rows or columns within a range that contains conditional formatting causes the new cells to have the same conditional formatting.
DELETING CONDITIONAL FORMATTING
When you press Delete to delete the contents of a cell, you do not delete the conditional formatting for the cell (if any). To remove all conditional formats (as well as all other cell formatting), select the cells and choose Home Editing Clear Clear Formats. Or, choose Home Editing Clear Clear All to delete the cell contents and the conditional formatting.
To remove only conditional formatting (and leave the other formatting intact), use Home Styles Conditional Formatting Clear Rules.
FIND AND REPLACE LIMITATIONS
Excel's Find And Replace dialog box includes a feature that allows you to search your worksheet to locate cells that contain specific formatting. This feature does not locate cells that contain formatting resulting from conditional formatting.
LOCATING CELLS THAT CONTAIN CONDITIONAL FORMATTING
Just by looking at a cell, you can't tell whether it contains conditional formatting. You can, however, use Excel's Go To dialog box to select such cells.
  1. Choose Home Editing Find & Select Go To Special.
  2. In the Go To Special dialog box, select the Conditional Formats option.
  3. To select all cells on the worksheet containing conditional formatting, select the All option. To select only the cells that contain the same conditional formatting as the active cell, select the Same option.
  4. Click OK. Excel selects the cells for you.

Creating Formula-Based Rules
Excel's conditional formatting feature is versatile, but sometimes it's just not quite versatile enough. Fortunately, you can extend its versatility by writing conditional formatting formulas.
The examples later in this section describe how to create conditional formatting formulas for the following:
  • To identify text entries
  • To identify dates that fall on a weekend
  • To format cells that are in odd-numbered rows or columns (for dynamic alternate row or columns shading)
  • To format groups of rows (for example, shading every group of two rows)
  • To display a sum only when all precedent cells contain values
  • To identify text cells that begin with the same first letter as a letter in a cell
  • To identify cells that contain a value that meets a criterion entered in a cell

Some of these formulas may be useful to you. If not, they may inspire you to create other conditional formatting formulas.
To specify conditional formatting based on a formula, select the cells and then choose Home Styles Conditional Formatting New Rule. This command displays the New Formatting Rule dialog box. Click the rule type labeled Use A Formula To Determine Which Cells To Format, and you'll be able to specify the formula.
You can type the formula directly into the box, or you can enter a reference to an existing formula. As with normal Excel formulas, the formula you enter here must begin with an equal sign (=).

Note 
The formula must be a logical formula that returns either TRUE or FALSE. If the formula evaluates to TRUE, the condition is satisfied, and the conditional formatting is applied. If the formula evaluates to FALSE, the conditional formatting is not applied.

UNDERSTANDING RELATIVE AND ABSOLUTE REFERENCES
If the formula that you enter into the Conditional Formatting dialog box contains a cell reference, that reference is considered a relative reference, based on the upper-left cell in the selected range.
For example, suppose that you want to set up a conditional formatting condition that applies shading to cells in range A1:B10 only if the cell contains text. None of Excel's conditional formatting options can do this task, so you need to create a formula that will return TRUE if the cell contains text and FALSE otherwise. Follow these steps:
  1. Select the range A1:B10 and ensure that cell A1 is the active cell.
  2. Choose Home Styles Conditional Formatting New Rule to display the New Formatting Rule dialog box.
  3. Click the rule type labeled Use a Formula to Determine Which Cells to Format.
  4. Enter the following formula in the Formula box:
5.  =ISTEXT(A1)
  1. Click the Format button to display the Format Cells dialog box.
  2. In the Format Cells dialog box, click the Fill tab and specify the cell shading that will be applied if the formula returns TRUE.
  3. Click OK to return to the New Formatting Rule dialog box.

In the New Formatting Rule dialog box, click the Preview button to make sure that the formula is working correctly and to see a preview of your selected formatting.
  1. If the preview looks correct, click OK to close the New Formatting Rule dialog box.

Notice that the formula entered in Step 4 contains a relative reference to the upper-left cell in the selected range.
Generally, when entering a conditional formatting formula for a range of cells, you'll use a reference to the active cell, which is normally the upper-left cell in the selected range. One exception is when you need to refer to a specific cell. For example, suppose that you select range A1:B10, and you want to apply formatting to all cells in the range that exceed the value in cell C1. Enter this conditional formatting formula:
=A1>$C$1
In this case, the reference to cell C1 is an absolute reference: It will not be adjusted for the cells in the selected range. In other words, the conditional formatting formula for cell A2 looks like this:
=A2>$C$1
The relative cell reference is adjusted, but the absolute cell reference is not.
USING REFERENCES TO OTHER SHEETS
If you enter a conditional formatting formula that uses one or more references to other sheets, Excel responds with an error message. If you need to refer to a cell on a different sheet, you must create a reference to that cell on the sheet that contains the conditional formatting. For example, if your conditional formatting formula needs to refer to cell A1 on Sheet3, you can insert the following formula into a cell on the active sheet:
=Sheet3!A1
Then use a reference to that cell in your conditional formatting formula.

Tip 
Another option is to create a name for the cell (by using Formulas Defined Names Define Name). After defining the name, you can use the name in place of the cell reference in your conditional formatting formula. If you use this technique, the named cell can be in any worksheet in the workbook.

CONDITIONAL FORMATTING FORMULA EXAMPLES
Each of these examples uses a formula entered directly into the New Formatting Rule dialog box, after selecting the rule type labeled Use a Formula to Determine Which Cells to Format. You decide the type of formatting that you apply conditionally.

On the CD 
The companion CD-ROM contains all the examples in this section. The file is named  conditional formatting formulas.xlsx.

Identifying Weekend Days
Excel provides a number of conditional formatting rules that deal with dates, but it doesn't let you identify dates that fall on a weekend. Use this formula to identify weekend dates:
=OR(WEEKDAY(A1)=7,WEEKDAY(A1)=1)
This formula assumes that a range is selected and also that cell A1 is the active cell.
Identifying Cells Containing More Than One Word
You also can use conditional formatting with text. For example, you can use the following conditional formatting formula to apply formatting to cells that contain more than one word:
=LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))>0
This formula assumes that the selected range begins in cell A1. The formula works by counting the space characters in the cell (using the TRIM function to strip out multiple spaces). If the count is greater than 0, the formula returns TRUE, and the conditional formatting is applied.
Displaying Alternate-Row Shading
The conditional formatting formula that follows was applied to the range A1:D18, to apply shading to alternate rows:
=MOD(ROW(),2)=0

Alternate row shading can make your spreadsheets easier to read. If you add or delete rows within the conditional formatting area, the shading is updated automatically.
This formula uses the ROW function (which returns the row number) and the MOD function (which returns the remainder of its first argument divided by its second argument). For cells in even-numbered rows, the MOD function returns 0, and cells in that row are formatted.
For alternate shading of columns, use the COLUMN function instead of the ROW function.
Creating Checkerboard Shading
The following formula is a variation on the example in the preceding section. It applies formatting to alternate rows and columns, creating a checkerboard effect.
=MOD(ROW(),2)=MOD(COLUMN(),2)
Shading Groups of Rows
Here's another rows shading variation. The following formula shades alternate groups of rows. It produces four rows of shaded rows, followed by four rows of unshaded rows, followed by four more shaded rows, and so on.
=MOD(INT((ROW()-1)/4)+1,2)

For different sized groups, change the 4 to some other value. For example, use this formula to shade alternate groups of two rows:
=MOD(INT((ROW()-1)/2)+1,2)
Displaying a Total Only When All Values Are Entered
shows a range with a formula that uses the SUM function in cell C6. Conditional formatting is used to hide the sum if any of the four cells above is blank. The conditional formatting formula for cell C6 (and cell C5, which contains a label) is
=COUNT($C$2:$C$5)=4
.
This formula returns TRUE only if C2:C5 contains no empty cells.

Identifing Text Cells That Begin with a Certain Letter
The worksheet contains a list of names in the range A5:G32. Cell A1 contains a letter of the alphabet. A conditional formatting formula highlights the names that begin with the letter in cell A1.

The conditional formatting formula for the range A5:G32 is
=LEFT(A5)=LEFT($A$1)
Identifying Cells That Meet a Numeric Criteria
The example in this section is similar to the previous example, but it involves values. The range A5:P32 uses the following conditional formatting formula:
=COUNTIF(A5,$A$1)=1
This formula takes advantage of the fact that the COUNTIF function can handle criteria that are entered in a cell. shows the worksheet when cell A1 contains the text >90.

Figure 19-23: Cells that meet the criteria entered in cell A1 are highlighted. 

USING CUSTOM FUNCTIONS IN CONDITIONAL FORMATTING FORMULAS
Excel's conditional formatting feature is very versatile. If it's not versatile enough, you can create your own formulas to define the conditions (as I explained in the previous sections). And if custom formulas still aren't versatile enough, you can create custom VBA function and use those in a conditional formatting formula.
This section provides three examples of VBA functions used in conditional formatting formulas.


On the CD 
The companion CD-ROM contains all the examples in this section. The file is named conditional formatting with VBA function.xlsm.

Identifying Formula Cells
Oddly, Excel does not have a function that determines whether a cell contains a formula. When Excel lacks a feature, you often can overcome the limitation by using VBA. The following custom VBA function uses the VBA HasFormula property. The function, which is entered into a VBA module, returns TRUE if the cell (specified as its argument) contains a formula; otherwise, it returns FALSE.
Function ISFORMULACELL(cell) As Boolean
    ISFORMULACELL = cell.HasFormula
End Function
After you enter this function into a VBA module, you can use the function in your worksheet formulas. For example, the following formula returns TRUE if cell A1 contains a formula:
=ISFORMULACELL(A1)
And you also can use this function in a conditional formatting formula.


Note 
Another way to identify formula cells is to use the Home Editing Find & Select Go To Special command, which displays the Go To Special dialog box. Choose the Formulas option and click OK to select all cells that contain a formula.

Identifying Date Cells
Excel also lacks a function to determine whether a cell contains a date. The following VBA function, which uses the VBA IsDate function, overcomes this limitation. The custom HASDATE function returns TRUE if the cell contains a date.
Function HASDATE(cell) As Boolean
    HASDATE = IsDate(cell)
End Function
The following conditional formatting formula applies formatting to cell A1 if it contains a date and the month is June:
=AND(HASDATE(A1),MONTH(A1)=6)
The following conditional formatting formula applies formatting to cell A1 if it contains a date and the date falls on a weekend:
=AND(HASDATE(A1),OR(WEEKDAY(A1)=7,WEEKDAY(A1)=1))

Identifying Invalid Data
You might have a situation in which the data entered must adhere to some very specific rules, and you'd like to apply special formatting if the data entered is not valid. You might have part numbers that consist of seven characters: four uppercase alphabetic characters, followed by a hyphen, and then a two-digit number-for example, ADSS-09 or DYUU-43.
You can write a conditional formatting formula to determine whether part numbers adhere to this structure, but the formula is very complex. The following formula, for example, returns TRUE only if the value in A1 meets the part number rules specified:
=AND(LEN(A1)=7,AND(LEFT(A1)>="A",LEFT(A1)<="Z"),
AND(MID(A1,2,1)>="A",MID(A1,2,1)<="Z"),AND(MID(A1,3,1)>="A",
MID(A1,3,1)<="Z"),AND(MID(A1,4,1)>="A",MID(A1,4,1)<="Z"),
MID(A1,5,1)="-",AND(VALUE(MID(A1,6,2))>=0,
VALUE(MID(A1,6,2))<=99))
For a simpler approach, write a custom VBA worksheet function. The VBA Like operator makes this sort of comparison relatively easy. The following VBA function procedure returns TRUE if its argument does not correspond to the part number rules outlined previously:
Function INVALIDPART(n) As Boolean
    If n Like "[A-Z][A-Z][A-Z][A-Z]-##" Then
INVALIDPART = False
    Else
       INVALIDPART = True
    End If
End Function
After defining this function in a VBA module, you can enter the following conditional formatting formula to apply special formatting if cell A1 contains an invalid part number:
=INVALIDPART(A1)
shows a range that uses the custom INVALIDPART function in a conditional formatting formula. Cells that contain invalid part numbers have a colored background.
In many cases, you can simply take advantage of Excel's data validation feature, which is described next.