The tutorial shows how to make custom Data Validation rules in Excel. You will find a few examples of Excel data validation formulas to allow only numbers or text values in specific cells, or only text beginning with specific characters, permit unique data preventing duplicates, and more.
In yesterday's tutorial we started to look at Excel Data Validation - what its purpose is, how it works, and how to use built-in rules to validate data in your worksheets. Today, we are going to take a step further and talk about the nitty-gritty aspects of custom data validation in Excel as well as experiment with a handful of different validation formulas.
How to create custom data validation with formula
Microsoft Excel has several built-in data validation rules for numbers, dates and text, but they cover only the most basic scenarios. If you want to validate cells with your own criteria, create a custom validation rule based on a formula. Here's how:
- Select one or more cells to validate.
- Open the Data Validation dialog box. For this, click the Data Validation button on the Data tab, in the Data Tools group or press the key sequence Alt > D > L (each key is to be pressed separately).
- On the Settings tab of the Data Validation dialog window, select Custom in the Allow box, and enter your data validation formula in the Formula box.
- Click OK.
Optionally, you can add a custom input message and Error alert that will show up when the user selects the validated cell or enters invalid data, respectively.
Below you will find a few examples of custom validation rules for different data types.
Note. All Excel data validation rules, built-in and custom, verify only new data that is typed in a cell after creating the rule. Copied data is not validated, nor is the data input in the cell before making the rule. To pin down existing entries that do not meet your data validation criteria, use the Circle Invalid Data feature as shown in How to find invalid data in Excel.
Excel data validation to allow numbers only
Surprisingly, none of the inbuilt Excel data validation rules cater for a very typical situation when you need to restrict users to entering only numbers in specific cells. But this can be easily done with a custom data validation formula based on the ISNUMBER function, like this one:
=ISNUMBER(C2)
Where C2 is the topmost cell of the range you want to validate.
Note. The ISNUMBER function allows any numeric values in validated cells, including integers, decimals, fractions as well as dates and times, which are also numbers in terms of Excel.
Excel data validation to allow text only
If you are looking for the opposite - to allow only text entries in given range of cells, then build a custom rule with the ISTEXT function, for example:
=ISTEXT(D2)
Where D2 is the uppermost cell of the selected range.
Allow text beginning with specific character(s)
If all values in a certain range should begin with a particular character or substring, then do Excel data validation based on the COUNTIF function with a wildcard character:
For example, to ensure that all order id's in column A begin with the "AA-", "aa-", "Aa-", or "aA-" prefix (case-insensitive), define a custom rule with this data validation formula:
=COUNTIF(A2,"aa-*")
Validation formula with the OR logic (multiple criteria)
In case there are 2 or more valid prefixes, add up several COUNTIF functions, so that your Excel data validation rule works with the OR logic:
=COUNTIF(A2,"aa-*")+COUNTIF(A2,"bb-*")
Case-sensitive validation formula
If the character case matters, then use EXACT in combination with the LEFT function to create a case-sensitive validation formula for entries beginning with specific text:
For instance, to allow only those order ids that begin with "AA-" (neither "aa-" nor "Aa-" is allowed), use this formula:
=EXACT(LEFT(A2,3),"AA-")
In the above formula, the LEFT function extracts the first 3 characters from cell A2, and EXACT performs a case-sensitive comparison with the hard-coded substring ("AA-" in this example). If the two substrings match exactly, the formula returns TRUE and the validation passes; otherwise FALSE is returned and the validation fails.
Allow entries containing certain text
To allow entries that contain specific text anywhere in a cell (in the beginning, middle, or end), use the ISNUMBER function in combination with either FIND or SEARCH depending on whether you want case-sensitive or case-insensitive match:
- Case-insensitive validation:
ISNUMBER(SEARCH(text, cell))
- Case-sensitive validation:
ISNUMBER(FIND(text, cell))
On our sample data set, to permit only entries containing the text "AA" in cells A2:A6, use one of these formulas:
Case-insensitive:
=ISNUMBER(SEARCH("AA", A2))
Case-sensitive:
=ISNUMBER(FIND("AA", A2))
The formulas work with the following logic:
You search the substring "AA" in cell A2 using FIND or SEARCH, and both return a position of the first character in the substring. If the text is not found, an error is returned. For any numeric value returned as the result of search, the ISNUMBER function yields TRUE, and data validation is successful. In case of an error, ISNUMBER returns FALSE, and the entry won't be allowed in a cell.
Data validation to allow only unique entries and disallow duplicates
In situations when a certain column or a range of cell should not contain any duplicates, configure a custom data validation rule to allow only unique entries. For this, we are going to use the classic COUNTIF formula to identify duplicates:
For example, to make sure that only unique order ids are input in cells A2 to A6, create a custom rule with this data validation formula:
=COUNTIF($A$2:$A$6, A2)<=1
When a unique value is entered, the formula returns TRUE and the validation succeeds. If the same value already exists in the specified range (count greater than 1), COUNTIF returns FALSE and the input fails validation.
Please pay attention that we lock the range with absolute cell references (A$2:$A$6) and use a relative reference for the top cell (A2) to get the formula to adjust properly for each cell in the validated range.
Note. This data validation formulas is case-insensitive, it does not distinguish uppercase and lowercase text.
Validation formulas for dates and times
Inbuilt date validation provides quite a lot of predefined criteria to restrict users to entering only dates between the two dates you specify, greater than, less than, or equal to a given date.
If you want more control over data validation in your worksheets, you can replicate the inbuilt functionality with a custom rule or write your own formula that goes beyond the built-in capabilities of Excel data validation.
Allow dates between two dates
To limit the entry to a date within a specified range, you can use either the predefined Date rule with the "between" criteria or make a custom validation rule with this generic formula:
Where:
- cell is the topmost cell in the validated range, and
- start and end dates are valid dates supplied via the DATE function or references to cells containing the dates.
For example, to allow only dates in the month of July of the year 2017, use the following formula:
=AND(C2>=DATE(2017,7,1),C2<=DATE(2017,7,31))
Or, enter the start date and end date in some cells (F1 and F2 in this example), and reference those cells in your formula:
=AND(C2>=$F$1, C2<=$F$2)
Please notice that the boundary dates are locked with absolute cell references.
Allow weekdays or weekends only
To restrict a user to entering only weekdays or weekends, configure a custom validation rule based on the WEEKDAY function.
With the return_type argument set to 2, WEEKDAY returns an integer ranging from 1 (Monday) to 7 (Sunday). So, for weekdays (Mon to Fri) the result of the formula should be less than 6, and for weekends (Sat and Sun) greater than 5.
Allow only workdays:
Allow only weekends:
For example, to allow entering only workdays in cells C2:C6, use this formula:
=WEEKDAY(C2,2)<6
Validate dates based on today's date
In many situations, you may want to use today's date as the start date of the allowed date range. To get the current date, use the TODAY function, and then add the desired number of days to it to compute the end date.
For example, to limit the data entry to 6 days from now (7 days including today), we are going to use the built-in Date rule with the formula-based criteria:
- Select Date in the Allow
- Select between in the Data
- In the Start date box, enter
=TODAY()
- In the End date box, enter
=TODAY() + 6
In a similar manner, you can restrict users to entering dates before or after today's date. For this, select either less than or greater than in the Data box, and then enter =TODAY()
in the End date or Start date box, respectively.
Validate times based on current time
To validate data based on the current time, use the predefined Time rule with your own data validation formula:
- In the Allow box, select Time.
- In the Data box, pick either less than to allow only times before the current time, or greater than to allow times after the current time.
- In the End time or Start time box (depending on which criteria you selected on the previous step), enter one of the following formulas:
- To validate dates and times based on the current date and time:
=NOW()
- To validate times based on the current time:
=TIME( HOUR(NOW()), MINUTE(NOW()), SECOND(NOW()))
- To validate dates and times based on the current date and time:
The screenshot below shows a rule that allows only times greater than the current time:
Custom Excel data validation rule not working
If your formula-based data validation rule does not work as expected, there are 3 main points to check:
- Data validation formula is correct
- Validation formula does not refer to an empty cell
- Appropriate cell references are used
Check the correctness of your Excel data validation formula
For starters, copy your validation formula into some cell to make sure it does not return an error such as #N/A, #VALUE or #DIV/0!.
If you are creating a custom rule, the formula should return the logical values of TRUE and FALSE or the values of 1 and 0 equating to them, respectively.
If you use a formula-based criteria in a built-in rule (like we did to validate times based on the current time), it can also return another numeric value.
Excel data validation formula should not refer to an empty cell
In many situations, if you select the Ignore blank box when defining the rule (usually selected by default) and one or more cells referenced in your formula is blank, any value will be allowed in the validated cell.
Here is an example in the simplest form:
Absolute and relative cell references in data validation formulas
When setting up a formula-based Excel validation rule, please keep in mind that all cell references in your formula are relative to the upper left cell in the selected range.
If you are creating a rule for more than one cell and your validation criteria are dependent on specific cells, be sure to use absolute cell references (with the $ sign like $A$1), otherwise your rule will work correctly only for the first cell. To better illustrate the point, please consider the following example.
Supposing, you want to restrict data entry in cells D2 to D5 to whole numbers between 1 (minimum value) and the result of dividing A2 by B2. So, you calculate the maximum value with this simple formula =A2/B2
, like shown in the screenshot below:
The problem is this seemingly correct formula won't work for cells D3 to D5 because relative references change based on a relative position of rows and columns. Thus, for cell D3 the formula will change to =A3/B3
, and for D4 it will become =A4/B4
, doing data validation all wrong!
To fix the formula, just type "$" before the column and row references to lock them: =$A$2/$B$2
. Or, press F4 to toggle between different reference types.
In situations when you want to validate each cell based on its own criteria, use relative cell references without $ sign to get the formula to adjust for each row or/and column:
As you see, there is no "absolute truth", the same formula could be right or wrong depending on situation and your particular task.
This is how to use data validation in Excel with your own formulas. T gain more understanding, feel free to download our sample workbook below and examine the rule settings. I thank you for reading and hope to see you on our blog next week!
Practice workbook for download
Excel Data Validation examples (.xlsx file)
286 comments
I have written a custom public function which returns a true or false value, and I have tested it in a cell and it works. But when I use the function in the formula field on the Data Validation Criteria pop up, I get this error: "A named range you specified cannot be found" The only range in my formula is a cell reference. Any help would be appreciated
Unfortunately, without seeing your data it hard to give you advice. For me to be able to help you better, please describe your task in more detail. Please specify what formula you used and what problem or error occurred. It’ll help me understand it better and find a solution for you. Thank you.
Hi team,
For some reason this validation rule is not triggering the stop alert in cell E9 when I enetr "X". $R$6=3 and I have entered "X" in three other worksheets in cell "E9". I wonder if you can help-
=SUMPRODUCT(COUNTIF(INDIRECT('Sheet names'!$E$2:$E$7&"!"&CELL("address",E9)),"*"&"X"&"*"))>$R$6
I figured it out. Just realized that the alert is triggered by "False" argument, not "true" argument. I just had to change the formula to "".
Hello!
I’m sorry but your task is not entirely clear to me.
Please describe your problem in more detail.Include an example of the source data and the result you want to get. It’ll help me understand your request better and find a solution for you. Thank you.
Hi,
I'm having difficulty with a custom Data Validation formula. I want it to check two nearby cells for existing values of 1; both these cells must not contain 1 (so if either does contain 1, the validation fails).
I've tried creating a macro but no matter what it does, it always fails to validate. I then tried entering a formula which works when tested in the sheet, but it always fails to validate when entered as a formula in Data Validation. Any clues please?
=AND(INDIRECT(ADDRESS(ROW(),COLUMN()+1,4))1,INDIRECT(ADDRESS(ROW(),COLUMN()+3,4))1)
Obviously the system here doesn't like my greater-than/lesser-than symbols as it's stripped them out of my formula!! Here it is again but without using those symbols:-
=AND(INDIRECT(ADDRESS(ROW(),COLUMN()+1,4))not equal to 1,INDIRECT(ADDRESS(ROW(),COLUMN()+3,4))not equal to 1)
Hello Teresa!
In the conditional formatting and data validation formula, the INDIRECT function does not work with the logical functions IF, AND, OR, and others. Therefore, I recommend that you divide your formula into 2 parts and create 2 conditions.
=INDIRECT(ADDRESS(ROW(),COLUMN()-1,4))<>1
and
=INDIRECT(ADDRESS(ROW(),COLUMN()+1,4))<>1
I hope this will help
Hello Dear Sir,
How can I do for folling,
"Data validation allows copy paste if it is validated number.If not, data validation reject this data."
How to restrict equal to sign (=) in excel cell
How do i lock a validation rule but can enter data in the cell
Hello!
It is not possible to block a data validation rule. It can only be deleted. More sophisticated data validation methods are possible using the VBA macro.
Dear Alexander,
Thank you for engaging to this thread once again. Despite the news are not what I was hopping for, still, excel carrying on the end result is something I kinda expected to tell you the truth.
If it wasn't necessary to have an alternative zero value in every cell, it would be much easier. I might write all the formulas I need manually, and create separate data validation for every cell. It might take me some time, but it'll totally worth the effort.
Thank you for your assistance. You're all great in Ablebits at what you're doing. Don't stop giving out essential and helpful hints!
Hello Ablebits team,
I'm having a hard time to figure out how to make options of a drop-down list, containing formulas, working with relative reference when I paste special with validation in another cell, without using VBA if possible.
For example, when I copy validation from cell P31, including a list option with the formula "=MIN($D31;$K31+L31)" and paste it to cell P33, I need this formula to change automatically into "=MIN($D33;$K33+L33)".
Could that be possible in anyway? Thank you in advance!
Hello Yannis!
When you copy cells, data validation formulas change in the same way as regular formulas. You can check it yourself.
Hello Alexander and thank you for your quick response,
I've tried it before and it seems not to be working, since the result I get by selecting this list component, is the same for every cell I paste the validation into.
Let me clarify that it's a list containing the formula (including the value zero and the formula) and not a custom validation, where I could write the formula directly into the box while creating it, in case this detail is of any importance.
Is there any worksheet setting I should turn on or off?
Yannis,
Thank you for the clarification. I mush have misunderstood your previous comment. I supposed you're using a custom validation formula applied to some data range and this formula is the one that doesn't get updated. If however, it is the range with different formulas you've set as the data validation source range, those formulas won't be recalculated when copying. You see, Excel uses not the formula but the end result for the validation, no matter where this validation list is.
I hope this will help, otherwise please do not hesitate to contact me anytime.
Hi,
I am working on the excel to validate specific texts cannot be entered into the cell. Example, when user keys in Hello or HELLO or bye or NA, the cell will show specific text entered is not allowed. If user keys other text, it will be allowed to be entered into the cell. Appreciate your help to list out the formula to resolve this probllem. Thanks
Column B Defines the scope of the product with two variables Yes or No e.g. Cell B1 = Yes/No
Column C - Cell C1 has data validation of three parameters Simple/Medium/Complex.
If the value of Cell B1 is Yes for the Scope; then only Cell C1 should get activated and the user should be able to choose the type of the parameter by clicking the drop-down arrow i.e. Simple/Medium/Complex.
Please advise the formula or procedure to do the same.
Best Regards
Hello Praveen!
I recommend reading these articles on drop-down lists in Excel and how to make a dependent drop-down list: https://www.ablebits.com/office-addins-blog/dependent-cascading-dropdown-lists-excel/
Hope this is what you need.
Dear All I am from India, please help me I got fade of to make this 'custom data validation isuue solved. I made a table having multiple column.
Column 'C' contains 'Items in innitial Stock', 'D' contains 'No. Of Used items', and 'E'containes 'Faulty items'.
Now three isuue are that (1) 'E' cant be greater than either the 'C'or 'C-D' and (2) if Stock 'C' is empty/blank no Entry should be allowed in 'E'.
And (3) if C is having 20 items and no item is used (it means 'D' is balnk), but some items foud faulty, so 'E' must not be greater than 'C'. And it must generate erro message if these condition violated.
I tried more than 50 Formula but it satisfied only 2 conditions.
plase help to make this formula.
Hello Santosh,
Please try the following formula for data validation for cell E3:
=IF(AND((C3-D3)>=E3,C3>0,C3>=E3), TRUE, FALSE)
You can learn more about Data Validation in Excel in this article on our blog:
https://www.ablebits.com/office-addins-blog/data-validation-excel/#custom-data-validation-rules
https://www.ablebits.com/office-addins-blog/excel-data-validation-custom-formula/
Hope you’ll find this information helpful.
Hello,
I am working on excel and for the specific columns i need to restrict the data-entry of " ."(space and dot, consecutively). For such validation which command should i use.
Help.
I'm trying to set up a validation rule to connect the value in one column to an appropriate valid value list in another column. For example, I have a validated cell (let's say B2) where the value is validate via a list on Sheet 2 in cells A1-A3. THe valid values in the list include three names: Paul (A1), Peter (A2) and Pamela (A3). Now, on sheet 2 I have a list of Paul's prefered toys in Cells C1-C10, Peter's list is in D1-D10 and Pamela's are in E1-E10. Now, back on Sheet 1 I want to create a validation formula in Cell B3 that says...... Refer to cell B2, if the value in the cell is the value in Sheet 2, Cell A1 then select from the list in Sheet 2, between cells C1-C10. If it's A2, then select from the list in sheet 2 cells D1-D10, and so forth. Is this possible? Another way to look at this is, I want to limit choice number 2 based on choice number 1. If I'm making a selection for Pauls' Christmas present, I only want to see the things on Pauls' Christmas list. Thanks for any help you can provide.
Hello Teri!
You should first select a particular name, and then get the list of toys based on this name. It can be easily done. For more details on how to solve this task, please have a look at this article: https://www.ablebits.com/office-addins-blog/dependent-cascading-dropdown-lists-excel/#cascading-dropdown-multiword
If you still have any questions, I will be glad to answer them.
Please Help me someone. How can I use two data validation in a column? I want only 9 digits to be entered in a cell and if it's more then 9 or less then 9 it should not accepted. and also this number should be uniqe in the column
To whom it may concern,
Question,
What is the formula for picking a month from a drop down list/pick list and enter the value for the start date(cell 1) and end date(cell 2) within that month.
For example:
1. I selected the month of the January from the drop down list and I will put value on cell 1 as the start date and cell 2 as the end date and will calculate the number of remaining days based on the formula =Networkdays(cell1, cell2).
That being said, only January dates will be acceptable as this is the current value selected in the pick list.
Thank you.
How can I disallow a value from being entered into one cell based on the value in a different cell? For example, if Cell A1 has the value "ABC" then no data can be allowed to be entered in cell B1 and a rejection message displayed if possible.
While doing a validation, can I have the If rule set i.e if the value in column A is selected then the appropriate list pertaining to the selected value only should appear in column B and others should be disabled.
Please help me in this case: I want to create the long formula for data validation but when i type the formula it was limited character. So what could i do to complete my data validation formula?
I am trying to use formula and list for data validation. For example, I have 2 columns, in the first column is the highest possible value can be chosen in the second column, I also want the user to choose from the list and not to type in their choice.
Whenever the user chooses a value greater than the value in the first column, there should be an error message. can this be done with data validation?
I want to mention in data validation, Yes,No or Other. But in other i want to put column blank as they will specify the requirement if they select other