This example teaches you how to use data validation to prevent users from entering duplicate values.
1. Select the range A2:A20.
2. On the Data tab, in the Data Tools group, click Data Validation.
3. In the Allow list, click Custom.
4. In the Formula box, enter the formula shown below and click OK.
Explanation: The COUNTIF function takes two arguments. =COUNTIF($A$2:$A$20,A2) counts the number of values in the range A2:A20 that are equal to the value in cell A2. This value may only occur once (=1) since we don’t want duplicate entries. Because we selected the range A2:A20 before we clicked on Data Validation, Excel automatically copies the formula to the other cells. Notice how we created an absolute reference ($A$2:$A$20) to fix this reference.
5. To check this, select cell A3 and click Data Validation.
As you can see, this function counts the number of values in the range A2:A20 that are equal to the value in cell A3. Again, this value may only occur once (=1) since we don’t want duplicate entries.
6. Enter a duplicate invoice number.
Result. Excel shows an error alert. You’ve already entered that invoice number.
Note: to enter an input message and error alert message, go to the Input Message and Error Alert tab.
Microsoft Excel is a powerful tool that can help you manage and organize large amounts of data. However, one frequent problem that many users face is the issue of duplicate entries. Duplicate entries are a big challenge while working with large data sets because they can skew your data and cause errors. Fortunately, In the Microsoft Excel, we have a feature data validation. It can help you prevent duplicate entries from being added to your data.
In this article, we will tell you how to prevent duplicate entries in Excel using data validation with formula.
Setting up Data Validation for Preventing Duplicates:
Data validation is a very powerful feature in Microsoft Excel. It can help you to prevent incorrect data entry into a cell or range of cells.
The first step to preventing duplicate entries is to select the range wherein you want to prevent duplicates. Once you have selected the range, you can access the Data validation feature in Excel by following these steps:
- Step 1: Go to the Data tab on the Excel ribbon.
- Step 2: Click on the Data Validation button in the Data Tools group or press shortcut key Alt+D+L
- Step 3: In the Data Validation dialog box, select the Settings tab.
- Step 4: Select the “Custom” validation option from the Allow dropdown menu.
- Step 5: In the Formula field, enter a formula that will prevent duplicates. For example, you can use the formula “=COUNTIF($A$1:$A$10,A1)=1“. This formula checks the range A1:A10 and ensures that the value being entered into the cell is unique.
- Step 6: Click on the OK button to save the data validation settings.
Once you have completed these steps, the data validation feature will be enabled for the selected range. Excel will prevent users from entering duplicate entries.
Using the Data Validation Feature to Prevent Duplicates in Real-life Scenarios:
Now as you know how to set up data validation to prevent duplicates, let’s have a look at some real-life scenarios where this feature can be useful.
Preventing Duplicate Names in a List:
One common scenario wherein you might want to prevent duplicates is while you are creating a list of names. For example, you might be creating a guest list for a party or a list of employees. To prevent duplicate entry of names, you can set up data validation for the range wherein you will enter names. In this case, you would select the range of cells wherein you will enter the names, use the same formula we used earlier as given below-
“=COUNTIF($A$1:$A$10,A1)=1”.
This formula will ensure that no duplicate names are entered into the list.
Preventing Duplicate Order Numbers in a Sales Log:
Another scenario wherein you might want to prevent duplicates is while you are tracking sales orders. In this case, you would want to prevent duplicate order numbers entry into the sales log. To do this, you would select the range of cells wherein you will enter the order numbers. Use this formula “=COUNTIF($A$1:$A$10,A1)=1”. It will ensure that each order number is unique and there are no duplicate entry into the sales log.
Preventing Duplicate Product Codes in an Inventory List:
If you are managing an inventory list, you might want to prevent duplicate product code entry. This can be important to ensure that you have accurate information about your inventory levels. To prevent duplicates, you need to select the range of cells wherein you will enter the product codes. Use the formula as given below –
"=COUNTIF($A$1:$A$10,A1)=1".
This will ensure that each product code is unique and that no duplicates are entered into the inventory list.
Preventing Duplicate Employee IDs in a Human Resources Database:
If you are managing a human resources database, then you might want to prevent duplicate employee IDs entry. This can be important to ensure that you have accurate information about your employees and their employment status. To prevent duplicates, you would select the range of cells wherein the employee IDs will be entered and use the formula as given below –
"=COUNTIF($A$1:$A$10,A1)=1".
This will ensure that each employee ID is unique and that no duplicates are entered into the HR database.
Additional Tips and Tricks for Using Data Validation to Prevent Duplicates:
In addition to the basic steps, we have outlined above, there are a few additional tips and tricks you can use to make data validation even more effective for preventing duplicates.
Using Named Ranges to Make Data Validation Easier to Manage:
One useful feature in Excel is Named ranges. Named ranges help to assign a name to a range of cells. Which makes it easier to manage your data validation settings. To create a named range, simply select the range of cells you want to name, then go to the Formulas tab and click on the Name Manager button. In the Name Manager dialog box, click on the “New” button to create a new named range. Use this named range in your data validation settings instead of the cell range. Press F3 shortcut Key to open the Paste name window in your data validation box.
Conclusion:
In conclusion, preventing duplicate entries is an important task. Make error less entry using data validation in Microsoft Excel. In this article, we have shown how to use data validation with formula to prevent duplicate entry. We have also explained a few real-life examples wherein this data validation feature can be useful. We have also explained additional tips and tricks to make data validation even more effective for preventing duplicates. You can ensure that your data is accurate and reliable after using these tips. It will help you make better decisions based on your data.
Watch the step-by-step video tutorial:
Click here to download the practice file
Author: Oscar Cronquist Article last updated on April 21, 2020
This article demonstrates how to set up Data Validation in order to control what the Excel user is allowed to enter. The condition is that there can’t be two identical records in the Excel Table.
The image above shows a warning that the Excel user tried to enter a duplicate record which is not valid, the dialog box tells you that «This value doesn’t match the data validation restrictions defined for this cell.
There are three buttons available, «Retry», «Cancel» and «Help» on the dialog box. The «Retry» button leaves the value as it is but selected, this allows you to edit the value you just entered. The «Cancel» button removes the value you just entered. The «Help» button opens a web page at Microsoft Support explaining how Data Validation works.
Note, it is still possible to copy and paste values to the Excel Table without the dialog box warning appearing. A green arrow in each cell corner of the record is now visible telling you that it is not valid.
Create an Excel Table
An Excel Table allows you to dynamically apply Data Validation to new data meaning if you enter a new record below the data set it will also have the same data validation rules automatically as the rest of the data.
With this setup there is no need to adjust cell ranges when new data is added or deleted, the Excel Tables does that for you instantly.
- Select any cell in the data set.
- Press shortcut keys CTRL + T to open the «Create Table» dialog box, see image above.
- Enable/disable checkbox «My table has headers» accordingly.
- Press with left mouse button on «OK» button to apply settings and create an Excel Table.
The data set is now an Excel Table which you can tell by the cell formatting and the arrows next to column headers. You can change the Excel Table style and remove «Filter» arrows next to column headers if you want.
A new tab on the ribbon named «Table Design» appears if you select on any cell in the Excel Table, it allows you to change Table Options and Styles.
Apply Data Validation
Data Validation lets you control what the Excel user can and can’t enter using different methods. We are going to use a «Data Validation» formula that will trigger a dialog box warning if conditions are not met.
- Select data in your Excel Table, I selected cell range B3:D9.
- Go to tab «Data» on the ribbon.
- Press with left mouse button on «Data validation» button.
- Choose Custom, see image above.
- Type in «Formula:» field:
=COUNTIFS(INDIRECT(«Table1[First name]»), $B3, INDIRECT(«Table1[Last name]»), $C3, INDIRECT(«Table1[Date]»), $D3)<=1
- Press with left mouse button on OK button to apply settings and create «Data Validation» to cell range B3:D8
If you enter a duplicate record, the following error message appears.
Explaining the data validation formula in row 9
To learn how formulas work in greater detail I recommend the «Evaluate Formula» tool which is built-in to Excel, you can use this tool for «Data Validation» formulas as well. Copy the «Data Validation» formula and paste to a cell.
I pasted the formula to cell F3 and then pressed Enter. Select cell F3, press with left mouse button on tab «Formulas» on the ribbon. Press with left mouse button on «Evaluate Formulas» button to show the «Evaluate Formula» dialog box, see image above.
The INDIRECT function is a volatile function meaning it recalculates every time Excel recalculates, this may make it more cpu-intensive if used extensively. This function is needed in order to reference values to an Excel Table in a Data Validation formula.
This is why you get the warning text «A function in this formula causes the result to change each time the spreadsheet is calculated. The final evaluation step will match the result in the cell, but interim steps may not» in the «Evaluate Formula» dialog box, see image above.
Underlined expressions are what is about to be evaluated in the next step and italic values are the result. Press with left mouse button on the «Evaluate» button to move to the next calculation step in the formula. Press with left mouse button on «Close» button to dismiss the dialog box.
Step 1 — How to reference values in Excel data validation formulas
You can’t reference Excel Tables in Data Validation formulas, however, there is a workaround. The INDIRECT function allows you to reference Excel Tables.
References to Excel Tables are called «structured references» and they don’t change when values or records are added or deleted in the Excel Table.
Table1[First name]
becomes
INDIRECT(«Table1[First name]»)
The downside is that you need to change the formulas if you change the Table name or the Table header names accordingly, they do not change automatically in this case.
Step 2 — Count how many times a record exists in a table
The COUNTIFS function lets you count cells based on multiple conditions, we are going to count rows based on the values the Excel user enters in the Excel Table.
COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]…)
We will use as many criteria pairs (ranges and criteria) as there are columns in the Excel Table. I will use six arguments as there are three columns in my Excel Table. You need to adjust that to your specific Excel Table.
COUNTIFS(INDIRECT(«Table1[First name]»), $B3, INDIRECT(«Table1[Last name]»), $C3, INDIRECT(«Table1[Date]»), $D3)
becomes
COUNTIFS($B$3:$B$9, $B3, $C$3:$C$9, $C3, $C$3:$D$9, $D3)
becomes
COUNTIFS({«Stieg»; «Jonas»; «Camilla»; «Lars»; «Henning»; «Jan»}, $B3, {«Larsson»; «Jonasson»; «Läckberg»; «Kepler»; «Mankell»; «Guillou»}, $C3, {40807; 41324; 41215; 40777; 41081; 40970}, $D3)
becomes
COUNTIFS({«Stieg»; «Jonas»; «Camilla»; «Lars»; «Henning»; «Jan» ; «Stieg»}, «Stieg», {«Larsson»; «Jonasson»; «Läckberg»; «Kepler»; «Mankell»; «Guillou»; «Larsson»}, «Larsson», {40807; 41324; 41215; 40777; 41081; 40970; 40807}, 40807)
and returns 1 in cell F3.
Step 3 — Check if number is smaller than or equal to 1
The less than sign and the equal sign together means that the number must be equal to or less than 1 in order to return True.
COUNTIFS(INDIRECT(«Table1[First name]»), $B3, INDIRECT(«Table1[Last name]»), $C3, INDIRECT(«Table1[Date]»), $D3)<=1
becomes
1<=1
and returns TRU. The «Data validation» error message does not appear.
Recommended articles
- Block duplicate entries in Excel table
- Apply data validation to cells
See all How-To Articles
This tutorial demonstrates how to prevent duplicate entries in Excel and Google Sheets.
Prevent Duplicate Entries
If you want to allow only unique values in a column, use the data validation functionality in Excel. This way, you can prevent a user from entering duplicate values. Say you have the following list of names in Column B.
To prevent duplicate entries in a range, follow these steps:
- Select the range for data validation (e.g., B2:B10), and in the Ribbon, go to Data > Data Validation.
- In the Data Validation window, (1) select Custom as the validation criteria under Allow, and (2) enter the COUNTIF formula:
=COUNTIF($B$2:$B$10,B2)=1
This formula counts – for each entry – how many times that value appears in the range B2:B10. If the result is 1 (meaning the entry appears only once in the range) then it is allowed. Otherwise, the data validation rule prevents a user from entering that value, since it is already in the range.
Then (3) go to the Input Message tab.
- In the Input Message tab, enter (1) the Title of the message you’re adding and (2) the Input message. This message appears when a user selects a cell in the data validation range to provide information about the data validation rule.
Then (3) go to the Error Alert tab.
- In the Error Alert tab, enter (1) the Title of the error message and (2) the Error message. This message appears if a user enters an existing value and prevent them from entering duplicate values.
Then (3) click OK.
Now selecting an empty cell from the data validation range (for example, B9) prompts an input message about data validation.
If you try to enter an existing value in cell B9 (e.g., Michael), you get the error message pictured below, stopping you from entering a duplicate value.
Note: To remove duplicate values or duplicate rows that were already present in the data, see How to Remove Duplicate Cells or this VBA tutorial.
Prevent Duplicate Entries in Google Sheets
To prevent duplicate entries in Google Sheets, follow these steps.
- Select the range for data validation (e.g., B2:B10), and in the Menu, go to Data > Data validation.
- In the Data validation window, (1) enter Custom formula is as the Criteria, and (2) enter the formula:
=COUNTIF($B$2:$B$10,B2)=1
Then (3) select Reject input, (4) check Show validation help text, and (5) enter an error message. (6) Click Save.
Now, if you try to enter a value in cell B9 that already exists in the range, you get the error message you just set.
on
October 2, 2018, 6:40 AM PDT
Use Excel data validation to prevent duplicate values in a column
Prevent duplicates before they happen by combining a simple function with data validation.
We may be compensated by vendors who appear on this page through methods such as affiliate links or sponsored partnerships. This may influence how and where their products appear on our site, but vendors cannot pay to influence the content of our reviews. For more info, visit our Terms of Use page.
Excel has built-in tools to expose and even delete duplicates, but they work on existing data after the fact. If you want to make sure duplicates never happen in the first place, you might consider using Excel’s data validation feature. This feature checks values as you enter them and depending on the rules you specify rejects or accepts that value. Unfortunately, there’s no built-in validation rule that recognizes a duplicate value, so you’ll need to combine the feature with Excel’s COUNTIF() function.
In this article, I’ll show you how to do this in a Table object using structured referencing and named ranges.
I’m using Office 365’s Excel 2016 (desktop) on a Windows 10 64-bit system, but both techniques will work in earlier versions and in the browser edition. You can work with your own data or download the demonstration .xlsx and .xls files.
SEE: Windows 10 power tips: Secret shortcuts to your favorite settings (Tech Pro Research)
About COUNTIF()
There’s no built-in duplicate rule for Excel’s Data Validation feature, but you can combine the feature with the COUNTIF() function to get the job done. To do so competently, you need to know about the COUNTIF() function. (Feel free to skip this section, if you know how to use this function.)
The COUNTIF() function counts the number of cells in a range that meet a specific condition. You supply the range and a condition as arguments using the following syntax:
COUNTIF(range,condition)
Let’s use this function to count the number of times a membership number occurs within column B, the Membership Number column, of the sheet shown in Figure A. Right now, this column allows duplicates.
Figure A
We’ll use data validation to prevent duplicate numbers in the Membership Number column.
First, enter the following function into cell K3:
=COUNTIF(Table1[Membership Number],B3)
The function uses structured referencing because the data is formatted as a Table object. Because the value 100 occurs only one time within the column, the function returns 1. Copy it to the remaining cells to see that they all return 1 (Figure A). If you repeat one of the values, the respective functions return 2, as shown in Figure B. You could use conditional formatting to warn you that a duplicate exists, but wouldn’t it be better to avoid the duplicate altogether?
Figure B
The function returns the number of times the condition is met.
Table accommodations
By adding the COUNTIF() function to the data validation settings, you can use this feature to reject a value if it already exists within range. Before you start, make sure the column in question contains no duplicates. We can illustrate this technique by adding such a validation data control to the Membership Number column as follows:
- Select all existing data cells in the column in question. In this case, that’s B3:B6.
- Click the Data tab, and choose Data Validation from the Data Validation dropdown (in the Data Tools group).
- In the resulting dialog, choose Custom from the Allow drop-down.
- In the Formula control, enter the formula (Figure C)
=COUNTIF(INDIRECT(“Table1[Membership Number]”),B3)<=1
making sure to use straight (not curly quotes). If you’re working with your own data, be sure to update the name of the Table and column. - Click OK.
Figure C
This custom rule will reject duplicates in the Membership Number column.
You don’t have to know exactly how the INDIRECT() function works, but briefly, it returns the reference as text. Without this function, the feature rejects the function because of the structured referencing necessary to accommodate Table objects. (You could enter the actual range, but you’d need to define a name for the range first. I’ll show you how to do that in the next section.)
Thanks to the Table and the INDIRECT() function, range increases every time you add a row. If you enter an existing value in any cell in range, the feature rejects it, as shown in Figure D. The error message isn’t particularly helpful unless you know about the duplicates rule; you can use the feature’s Input Message and Error Alert tabs (see Figure B) to display meaningful information to your users.
Figure D
The custom rule rejects a value if it already exists in range.
Named range
There’s no reason to avoid INDIRECT(), but you can use named ranges instead. You can apply a name to the existing data cells as follows:
- Select B3:B6.
- Enter MNumber in the Name control (Figure E).
- Press Enter. You must press Enter to commit the name.
Figure E
Use the Name control to name a range.
Next, create a validation rule as you did before, but enter the following function, which references the named range instead of using INDIRECT():
=COUNTIF(MNumber,B3)<=1
The two rules work the same, but one works with the Table structure, one works with a named range.
Send me your question about Office
I answer readers’ questions when I can, but there’s no guarantee. Don’t send files unless requested; initial requests for help that arrive with attached files will be deleted unread. You can send screenshots of your data to help clarify your question. When contacting me, be as specific as possible. For example, “Please troubleshoot my workbook and fix what’s wrong” probably won’t get a response, but “Can you tell me why this formula isn’t returning the expected results?” might. Please mention the app and version that you’re using. I’m not reimbursed by TechRepublic for my time or expertise when helping readers, nor do I ask for a fee from readers I help. You can contact me at susansalesharkins@gmail.com.
See also
- A super easy way to generate new records from multi-value columns using Excel Power Query (TechRepublic)
- 3 ways to add glossary terms to a Microsoft Word 2016 document (TechRepublic)
- Excel errors: How Microsoft’s spreadsheet may be hazardous to your health (ZDNet)
- Five tips for using Outlook 2016’s AutoComplete list efficiently (TechRepublic)
- How to use VBA to select an Excel range (TechRepublic)
-
Software