Preventing Duplicates On Entry

来源:百度文库 编辑:神马文学网 时间:2024/06/03 05:46:35
In Excel97 and later versions, you can use the Data Validation feature to prevent a user from entering a number (or text) in a cell, if that  number already exists in a list.  This feature is not available in versions of Excel before Excel97.
For example, suppose your worksheet will contain a list of ID numbers for employees, in cells A1:A50, and you want to prevent the user from entering duplicate numbers in the list. First, select the range A1:A50.  Then, go to the Data menu, and select the Validation menu item.  This will display the Data Validation dialog.  From the "Allow" drop down box, select Custom, and enter the following formula in the Formula box:
=COUNTIF($A$1:$A$50,A1)=1

Then, select the "Error Alert" tab, and enter the message you want to display when the user enters a duplicate value. Finally, press OK.  You must use absolute references in the range, and relative references in the criteria, as shown in the example.
Remember, that Data Validation is in effect only when the cell value is changed by the user.  It does not catch changes made by VBA procedures, or as a result of a calculation in a formula. Also, Data Validation does not apply to changes made when the user pastes data in to the range.
Other Excel formulas and VBA procedures for working with duplicate and unique entries in lists can be found on the following pages:
Preventing Duplicates On Entry 
Duplicates
Deleting Rows