In This Chapter
This chapter explores a useful Excel feature: data validation. Data validation enables you to add rules for what’s acceptable in specific cells and allows you to add dynamic elements to your worksheet without using macro programming.
The Excel data validation feature allows you to set up rules that dictate what can be entered into a cell. For example, you may want to limit data entry in a particular cell to whole numbers between 1 and 12. You can specify an input message to help the user know what kind of data to enter in the cell. If the user makes an invalid entry, you can display a custom message. Figure 20.1 shows an example of both the input message and the message generated from an invalid entry.
Excel makes it easy to specify the validation criteria, and you can also use a formula for more complex criteria.
To specify the type of data allowable in a cell or range, follow these steps while you refer to Figure 20.2, which shows all three tabs of the Data Validation dialog box:
(Optional) Click the Error Alert tab and specify which error message to display when a user makes an invalid entry. The selection for Style determines what choices users have when they make invalid entries. To prevent an invalid entry, choose Stop. If this step is omitted, a standard message will appear if the user makes an invalid entry.
From the Settings tab of the Data Validation dialog box, you can specify a variety of data validation criteria. The following options are available from the Allow drop-down list. Keep in mind that the other controls on the Settings tab vary, depending on your choice from the Allow drop-down list.
The Settings tab of the Data Validation dialog box contains two other check boxes:
Perhaps one of the most common uses of data validation is to create a drop-down list in a cell. Figure 20.4 shows an example that uses the month names in A1:A12 as the list source.
To create a drop-down list in a cell, do the following:
For simple data validation, the data validation feature is quite straightforward and easy to use. The real power of this feature, though, becomes apparent when you use data validation formulas.
We present several examples of formulas used for data validation in the upcoming section “Data Validation Formula Examples.”
If the formula that you enter into the Data Validation dialog box contains a cell reference, that reference is considered a relative reference, based on the upper-left cell in the selected range.
The following example clarifies this concept. Suppose that you want to allow only an odd number to be entered into the range B2:B10. None of the Excel data validation rules can limit entry to odd numbers, so a formula is required.
Follow these steps:
Enter the following formula in the Formula field, as shown in Figure 20.5:
=ISODD(B2)
This formula uses the ISODD function, which returns TRUE if its numeric argument is an odd number. Notice that the formula refers to the active cell, which is cell B2.
Notice that the formula entered contains a reference to the upper-left cell in the selected range. This data validation formula was applied to a range of cells, so you might expect that each cell would contain the same data validation formula. Because you entered a relative cell reference as the argument for the ISODD function, Excel adjusts the formula for the other cells in the B2:B10 range. To demonstrate that the reference is relative, select cell B5 and examine its formula displayed in the Data Validation dialog box. You’ll see that the formula for this cell is
=ISODD(B5)
Generally, when entering a data validation formula for a range of cells, you use a reference to the active cell, which is normally the upper-left cell in the selected range. An exception is when you need to refer to a specific cell. For example, suppose that you select range A1:B10 and you want your data validation to allow only values that are greater than the value in cell C1. You would use this 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, which is just what you want. The data validation formula for cell A2 looks like this:
=A2>$C$1
The relative cell reference is adjusted, but the absolute cell reference is not.
The following sections contain a few data validation examples that use a formula entered directly into the Formula control on the Settings tab of the Data Validation dialog box. These examples help you understand how to create your own data validation formulas.
Excel has a data validation option to limit the length of text entered into a cell, but it doesn’t have an option to force text (rather than a number) into a cell. To force a cell or range to accept only text (no values), use the following data validation formula:
=ISTEXT(A1)
This formula assumes that the active cell in the selected range is cell A1.
The following data validation formula enables the user to enter a value only if it’s greater than the value in the cell directly above it:
=A2>A1
This formula assumes that A2 is the active cell in the selected range. Note that you can’t use this formula for a cell in row 1.
The following data validation formula does not permit the user to make a duplicate entry in the range A1:C20:
=COUNTIF($A$1:$C$20,A1)=1
This is a logical formula that returns TRUE if the value in the cell occurs only one time in the A1:C20 range. Otherwise, it returns FALSE, and the Duplicate Entry dialog box is displayed.
This formula assumes that A1 is the active cell in the selected range. Note that the first argument for COUNTIF is an absolute reference. The second argument is a relative reference, and it adjusts for each cell in the validation range. Figure 20.6 shows this validation criterion in effect, using a custom error alert message. The user is attempting to enter a value into cell B5 that already exists in the A1:C20 range.
The following data validation formula demonstrates how to check for a specific character. In this case, the formula ensures that the user’s entry is a text string that begins with the letter A (uppercase or lowercase):
=LEFT(A1)="a"
This is a logical formula that returns TRUE if the first character in the cell is the letter A. Otherwise, it returns FALSE. This formula assumes that the active cell in the selected range is cell A1.
If case sensitivity is important and you want to limit entries to only those that start with a lowercase a, you can use the EXACT worksheet function as shown here:
=EXACT(LEFT(A1,1),"a")
The following formula is a variation of the non–case sensitive validation formula. It uses wildcard characters in the second argument of the COUNTIF function. In this case, the formula ensures that the entry begins with the letter A and contains exactly five characters:
=COUNTIF(A1,"A????")=1
The following data validation formula ensures that the cell entry is a date and that the date is a Monday:
=WEEKDAY(A1)=2
This formula assumes that the active cell in the selected range is cell A1. It uses the WEEKDAY function, which returns 1 for Sunday, 2 for Monday, and so on.
Figure 20.7 shows a simple budget worksheet, with the budget item amounts in the range B1:B6. The planned budget is in cell E5, and the user is attempting to enter a value in cell B4 that would cause the total (cell E6) to exceed the budget. The following data validation formula ensures that the sum of the budget items does not exceed the budget:
=SUM($B$1:$B$6)<=$E$5
As we described previously, you can use data validation to create a drop-down list in a cell (see “Creating a Drop-Down List”). This section explains how to use a drop-down list to control the entries that appear in a second drop-down list. In other words, the second drop-down list is dependent upon the value selected in the first drop-down list.
Figure 20.8 shows a simple example of a dependent list created by using data validation. Cell E2 contains data validation that displays a three-item list from the range A1:C1 (Vegetables, Fruits, and Meats). When the user chooses an item from the list, the second list (in cell F2) displays the appropriate items. Cell E2 contains data validation with its Allow criterion set to List and its Source criterion shown below:
=$A$1:$C$1
This worksheet uses three named ranges:
For this technique to work, your range names must match the values in A1:C1. For example, if you type Veggies in cell A1 and name the range Vegetables, the list in F2 won’t contain items. Cell F2 contains data validation that uses this formula:
=INDIRECT($E$2)
The INDIRECT function converts its argument into a range if it can. If cell E2 contains the text Fruits, for instance, INDIRECT looks for a range named Fruits and supplies that range to the data validation list.
With Excel Tables (Insert ➜ Table) you can use structured table referencing in your formulas. Figure 20.9 shows a simple table consisting of one column of names. You can create a formula like this one that matches a name to the list:
=MATCH("Joe",Table1[Name],FALSE)
The second argument, Table1[Name], is a structured reference that returns the Name column of the table named Table1. As you add or remove rows to that table, the structured reference adjusts and your formulas continue to work.
Unfortunately, you can’t use structured table references in your data validation criteria. To overcome this, you can create a named range that uses a structured table reference and use that name in your data validation.
To create a named range, choose Formulas ➜ Defined Names ➜ Define Name for the Ribbon. Enter the name, such as dvTable1Name, and point to the table’s column in the Refers To box. The reference changes to =Table1[Name], as shown in Figure 20.10.
Now you can use dvTable1Name in your data validation to get a list that grows or shrinks with the table. Figure 20.11 shows a cell with such validation.
This example used a naming convention of dv plus the table name plus the column name. You don’t have to use a convention like this; you can use any valid named range.
18.226.164.53