Prevent invalid data entry in a worksheet
In many worksheets that you create, users will enter data to get the desired calculations and results. Ensuring valid data entry is an important task. You may want to restrict data entry to a certain range of dates, limit choices by using a list, or make sure that only positive whole numbers are entered. Providing immediate help to instruct users and clear messages when invalid data is entered is also essential to make the data entry experience go smoothly.
Once you decide what validation you want to use on a worksheet, you can set up the validation by doing the following:
- Select one or more cells to validate.
- On the
Datatab, in theData Toolsgroup, clickData Validation.
The
Data Validationdialog box is displayed. - Click the
Settingstab. - To specify the type of validation that you want, do one of the following:
Allow values from a list
- In the
Allowbox, selectList. - Click the
Sourcebox and then type the list values separated by the Microsoft Windows list separator character (commas by default).For example:
- To limit entry to a question, such as "Do you have children?", to two choices, type Yes, No.
- To limit a vendor's quality reputation to three ratings, type Low, Average, High.
You can also create a list of values from a range of cells. For more information, see Create a drop-down list from a range of cells.
Note The width of the drop-down list is determined by the width of the cell that has the data validation. You may need to adjust the width of that cell to prevent truncating the width of valid entries that are wider than the width of the drop-down list.
- Make sure that the
In-cell dropdowncheck box is selected.
Allow a whole number within limits
- In the
Allowbox, selectWhole Number. - In the
Databox, select the type of restriction that you want. For example, to set upper and lower limits, selectbetween. - Enter the minimum, maximum, or specific value to allow. You can also enter a formula that returns a number value.
For example, to set a minimum limit of deductions to two times the number of children in cell F1, select
greater than or equal toin theDatabox and enter the formula,=2*F1, in theMinimumbox.
Allow a decimal number within limits
- In the
Allowbox, selectDecimal. - In the
Databox, select the type of restriction that you want. For example, to set upper and lower limits, selectbetween. - Enter the minimum, maximum, or specific value to allow. You can also enter a formula that returns a number value.
For example, to set a maximum limit for commissions and bonuses of 6% of a salesperson's salary in cell E1, select
less than or equal toin theDatabox and enter the formula,=E1*6%, in theMaximumbox.Note To allow a user to enter percentages, for example 20%, select
Decimalin theAllowbox, select the type of restriction that you want in theDatabox, enter the minimum, maximum, or specific value as a decimal, for example .2, and then display the data validation cell as a percentage by selecting the cell and clickingPercent Style
in the Numbergroup on theSheettab.
Allow a date within a timeframe
- In the
Allowbox, selectDate. - In the
Databox, select the type of restriction that you want. For example, to allow dates after a certain day, selectgreater than. - Enter the start, end, or specific date to allow. You can also enter a formula that returns a date.
For example, to set a time frame between today's date and 3 days from today's date, select
betweenin the Data box, enter =TODAY() in theMinimumbox, and enter =TODAY()+3 in theMaximumbox.
Allow a time within a timeframe
- In the
Allowbox, selectTime. - In the
Databox, select the type of restriction that you want. For example, to allow times before a certain time of day, selectless than. - Enter the start, end, or specific time to allow. You can also enter a formula that returns a time value.
For example, to set a time frame for serving breakfast between the time when the restaurant opens in cell H1 and 5 hours after the restaurant opens, select
betweenin the Data box, enter =H1 in theMinimumbox, and then enter =H1+"5:00" in theMaximumbox.
Allow text of a specified length
- In the
Allowbox, selectText Length. - In the
Databox, select the type of restriction that you want. For example, to allow up to a certain number of characters, selectless than or equal to. - Enter the minimum, maximum, or specific length for the text. You can also enter a formula that returns a number value.
For example, to set the specific length for a full name field (C1) to be the current length of a first name field (A1) and a last name field (B1) plus 10, select
less than or equal toin theDatabox and enter =SUM(LEN(A1),LEN(B1),10) in theMaximumbox.
Calculate what's allowed based on the content of another cell
- In the
Allowbox, select the type of data that you want. - In the
Databox, select the type of restriction that you want. - In the box or boxes below the
Databox, click the cell that you want to use to specify what's allowed.For example, to allow entries for an account only if the result won't go over the budget in cell E4, select
DecimalforAllow, selectless than or equal toforData, and in theMaximumbox, enter =E4.
Use a formula to calculate what's allowed
- In the
Allowbox, selectCustom. - In the
Formulabox, enter a formula that calculates a logical value (TRUE for valid or FALSE for invalid entries). For example:To make sure that Enter this formula The cell for the picnic account (B1) can only be updated if nothing is budgeted for the discretionary account (D1) and the total budget (D2) is less than the $40,000 allocated. =AND(D1=0,D2<40000)The cell containing a product description (B2) only contains text. =ISTEXT(B2)For the cell containing a projected advertising budget (B3), the subtotal for subcontractors and services (E1) must be less than or equal to $800, and the total budget amount (E2) must also be less than or equal to $97,000. =AND(E1<=800,E2<=97000)The cell containing an employee age (B4) is always greater than the number of full years of employment (F1) plus 18 (the minimum age of employment). =IF(B4>F1+18,TRUE,FALSE)All the data in the cell range A1:A20 contains unique values. =COUNTIF($A$1:$A$20,A1)=1You must enter the formula in the data validation for cell A1, and then fill the cells A2 though A20 so that the data validation for each cell in the range has a similar formula, but the second argument to the COUNTIF function will match the current cell.
The cell containing a product code name (B5) always begins with the standard prefix of ID- and is at least 10 characters in length. =AND(LEFT(B5, 3) ="ID-",LEN(B5) > 9)
- In the
- To specify how you want to handle blank (null) values, select or clear the
Ignore blankcheck box.Note If your allowed values are based on a cell range with a defined name, and there is a blank cell anywhere in the range, setting the
Ignore blankcheck box allows any values to be entered in the validated cell. This is also true for any cells referenced by validation formulas: if any referenced cell is blank, setting theIgnore blankcheck box allows any values to be entered in the validated cell. - Optionally, display an input message when the cell is clicked.
How?
- Click the
Input Messagetab. - Make sure the
Show input message when cell is selectedcheck box is selected. - Fill in the title and text for the message.
- Click the
- Specify how you want Microsoft Office Excel to respond when invalid data is entered.
How?
- Click the
Error Alerttab, and make sure that theShow error alert after invalid data is enteredcheck box is selected. - Select one of the following options for the
Stylebox:- To display an information message that does not prevent entry of invalid data, select
Information. - To display a warning message that does not prevent entry of invalid data, select
Warning. - To prevent entry of invalid data, select
Stop.
- To display an information message that does not prevent entry of invalid data, select
- Fill in the title and text for the message (up to 225 characters).
Note If you don't enter a title or text, the title defaults to "Microsoft Excel" and the message to: "The value you entered is not valid. A user has restricted values that can be entered into this cell."
- Click the
- Test the data validation to make sure that it's working correctly.
Try entering both valid and invalid data in the cells to make sure that your settings are working as you intended and your messages are appearing when you expect.
Tip If you make changes to the validation in one cell, you can automatically apply your changes to all other cells that have the same settings.
Notes:
- Apply protection, if needed If you are planning to protect the worksheet or workbook, protect it after you have finished setting up validation. Make sure that you unlock any validated cells before protecting the worksheet, otherwise users won't be able to type in the cells.
- Share the workbook, if needed If you are planning to share the workbook, share it after you have finished setting up validation and protection. After you share a workbook, you won't be able to change the validation settings unless you stop sharing, but Excel will continue to validate the cells that you have designated while the workbook is shared.