Excel Data Validation: custom formulas and rules

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:

  1. Select one or more cells to validate.
  2. 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).
  3. 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.
  4. Click OK.

Creating a custom formula-based validation rule in Excel

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.
A custom data validation rule to allow numbers only

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.
A custom data validation rule to allow text only

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:

COUNTIF(cell,"text*")

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-*")
Data validation to allow text beginning with specific characters

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-*")
Excel data validation formula with the OR logic

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:

EXACT(LEFT(cell, number_of_chars), 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.
Case-sensitive validation formula to allow text beginning with specific characters

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 entries containing certain text

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:

=COUNTIF(range, topmost_cell)<=1

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.
Data validation to allow only unique entries

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:

AND(cell>=start_date), cell<=end_date)

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.
Data validation to allow dates between two dates

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:

WEEKDAY(cell,2)<6

Allow only weekends:

WEEKDAY(cell,2)>5

For example, to allow entering only workdays in cells C2:C6, use this formula:

=WEEKDAY(C2,2)<6
Validation rule to allow only workdays

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:

  1. Select Date in the Allow
  2. Select between in the Data
  3. In the Start date box, enter =TODAY()
  4. In the End date box, enter =TODAY() + 6

Validating dates based on today's date

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:

  1. In the Allow box, select Time.
  2. 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.
  3. 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()))

The screenshot below shows a rule that allows only times greater than the current time:
Validating times based on 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:
Validation formula should not refer to an empty cell

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:
Incorrect cell references in a data validation formula

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:
Correct cell references in a data validation formula

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

  1. Hi,

    I created a data validation drop down list.

    A: Date not synced
    B: Term
    C: Item
    D: Amount
    E: Combination: ________

    Like to ask your help on how to put a formula, when I pick "E" I can write a comment on the it. Like for example, combination of A&B or C&D or ABC or combination of everything

    • Hi!
      You cannot change the values that are inserted into a cell using a drop-down list. You can create in your dropdown list more options for item e and write down all the possible combinations.

  2. I need to restrict a field using Data Validation so that the user can only enter a Proper Name which means it must be entered using uppercase letters, numbers and only the following 4 special characters
    ,(comma)
    -(dash)
    & (Ampersand sign)
    ' (Apostrophe)
    How would I do this? in a long statement specifying all the valid characters, or in a table that is 1 column with all the valid characters?

  3. Hi!! I'm really dumb at such things like this, I'm in my 1st yr of college and we need to do data validation. C2:C7 contains the salary ( which the criteria, the amount must be 12, 000-35,000 ), and the next cell D2:D7 contains the bonus salary. The bonus salary must be 12% of the salary or cannot be greater than 12%. I know I need to make a custom formula but I'm really dumb at logic and maths, please help. Thank you and I apologize for my dumbness, have a great day <3

    • Hi!
      The formula returns TRUE if the bonus salary does not exceed 12% of the basic salary:

      =IF(D2/C2<0.12,TRUE,FALSE)

      or

      =D2/C2<0.12

      Please check out this article to learn how to create conditions with IF function in Excel.

  4. Hi,
    How to restrict any profile in validation rule formula, that means I don't need to run that user in validation rule.. So any formula

  5. In my drop-down list i have 3 options (1,2.3), from this when i have choose 1 and 3 i should receive error message. when choose 2 i don't want error message. can you help me.

  6. One more item in the list can be validating field based on conditional values from other fields.

    For example:
    1. Value in Sheet2!B2 should be a date greater than or equal to the date found using vlookup of Sheet2!A2 on Range Sheet1!A:D for D column, and less than similar vlookup on another range.
    2. Value in B2 should be some percentage of sum of value from some other range.
    3. Value in B2 should be either from the list from some other range, or should be from a list from some other range 2.
    4. Value in B3 should be in (the list/range + B2), and so on... That is, adding previous value to the list in the data validation.

    These kind of validations are more common in our day to day work. Simple list validations or date validations are very simple to use.

  7. Hi Alexander, thanks for your answer.

    I have done the regular drop-down list.
    Now, what I need is to be able to select data from the list in cell A1 and get it to show me "blue", whilst on cell B1, I select the same item on the same list but I get it to show me "1".
    Do you understand what I mean?

    Thanks for your help.

  8. Hi there,

    I have a cell with a data validation list dropdown. I want to make a rule to get the cell to show something different when a particular value of the list is selected. Is this possible?

    For example, let's say I have a list from 1 to 10. I want the list to show all the numbers normally, but I want the 1 to show "Improvement needed!" and the 10 to show "Awesome!", however, I need the list to still be 1 to 10, as I would need to do other operations with the data of that list.

    Thanks for your help.

    • Hello!
      A regular drop-down list inserts the data it shows into the cell.
      An ActiveX ComboBox (Developer - Insert menu tab) may be suitable for your task.

  9. I want the cell to except only Capital M. How would I that?

    Thanks

    • Hello!
      I hope you have studied the recommendations in the tutorial above. It contains answers to your question.
      Formula =A1="Capital M"

  10. Can I restrict the entry to one decimal point? The problem is even if I set the format of cell as number with one decimal point, when I enter 0.12, the value is still stored as 0.12 despite the number show in the cell is 0.1.

  11. Column D has data validation rule to select from a drop down list.

    I wish column E to automatically fill based on a custom rule:

    =VLOOKUP(D2,Suppliers!A:B,2,FALSE) /* No errors when setting up the custom rule.*/

    But when i select a value from the drop down list for cell D2, nothing fills in cell E2.

    Thanks for your feedback.

    • Hello!
      Unfortunately, without seeing your data it is difficult to give you any advice. Please check out the following article on our blog, it’ll be sure to help you with your task: Excel VLOOKUP not working.
      If there is anything else I can help you with, please let me know.

      • Hello,

        This is the dropdown list worksheet called Suppliers:
        ----------------------------------------------------------------------------
        Allen & Sons Ltd Water
        Beacham & Sons Ltd Electricity
        Chisholm & Sons Ltd Medical Equipment
        Davidson & Sons Ltd Building Maintenance
        Elias & Sons Ltd Computer Equipment
        Fanshaw & Sons Ltd Window Cleaning
        George & Sons Ltd Gardening

        This is the data entry worksheet:
        -----------------------------------------------
        Supplier (col D) Supplies (col E)
        Allen & Sons Ltd
        Beacham & Sons Ltd
        Chisholm & Sons Ltd
        Davidson & Sons Ltd
        Elias & Sons Ltd

        When I select Allen & Sons Ltd in the Supplier column D, I expected column E to fill automatically with the word Water because of the custom data validation formula, but it remains blank.
        =VLOOKUP(D2,Suppliers!A:B,2,FALSE)

        I know I can make cell E do a VLOOKUP, but I was testing to see if the VLOOKUP function could be incorporated as part of a custom data validation formula.

        This is not a problem, it's just a technical challenge.

        Thank you.

        • Hello!
          Dropdown list in D1 cannot insert value in E1. In cell E1, you can write down the VLOOKUP formula. Once you select a value in D1, it will find the value you want.
          I hope I answered your question. If something is still unclear, please feel free to ask.

  12. Hi
    Using data validation, I know it is possible to do the below separately:

    1. Create a data validation drop down list (ALT + A + V + V then select list from the 'Allow' field and denote the cell range in the 'Source' field)
    2. Prevent users from entering data into one cell (e.g. cell A2) if another cell (e.g. cell A1) has not yet been populated (ALT + A + V + V then select Custom from the 'Allow' field and insert '=NOT(ISBLANK(A1))' in the 'Formula' field)

    However, I am in need of a combination of both. I need to customise a cell such that if cell A1 is not populated, then cell A2 cannot be populated but, when cell A1 has data inserted, A2 should be able to be populated but only through a drop-down list. May you assist

    • Using data validation, I know it is possible to do the below separately:
      ...
      2. Prevent users from entering data into one cell (e.g. cell A2) if another cell (e.g. cell A1) has not yet been populated (ALT + A + V + V then select Custom from the 'Allow' field and insert '=NOT(ISBLANK(A1))' in the 'Formula' field)
      ...

      How do you know that?? It's NOT working!

      ...Does anyone know if it's possible to use as a condition in formula of Data Validation empty cells??

  13. Does data validation does not read result in formula result?

  14. Hi,

    Just wondering if you can help me with something. I'm trying to create a variable list dependent on a variable value. E.g. If cell equals a number then give me list of job titles relating to that number.
    I say its variable because the number depends on which department is selected. The job titles are on a separate worksheet within the workbook. The list is variable because job titles can be added or removed to the list so I can't label specific ranges.
    Essentially I want to do a vlookup but the result being a list rather than a single value... is this possible and if so please let me know how to do this!

  15. First row of my spreadsheet is the column titles.
    In row 2 I have four Data Validation drop downs, each column drop down is dependent upon whichever item is picked in the drop down in a previous cell in the same row, but not necessarily adjacent columns. All is working correctly in row 2
    The spreadsheet has 500 data entry rows to allow recording of approx. 6 to 10 jobs per day. How do I extend the Data Validation List rules to all 500 rows?
    I have used Copy/Paste Special/Validation on each column but this is not working correctly. Is the copy/paste method I am using the wrong one for this application?

    • Above issue resolved. Was a syntax error in one of the formulae where $C$4 was locking drop-down lists to a single cell.

    • Hello!
      The information you provided is not enough to understand your case and give you any advice.
      What data validation formula are you using? Specify in more detail what problem occurs in this case.

  16. Hi,

    We have a spreadsheet that currently monitors the jobs our contractors are at, and the total hours worked overall for the week. We have the list of employees on the one sheet, and we use the Data Validation drop down to select the jobs for the hours, and I was wondering if there is a formula to also calculate the number of hours based on each specific job, as well as the overall total hours I have listed.

    Thanks.

  17. I want to know formula to list down the data validation list when x collum value is Yes else it shoud show not applicable. please help

    • Hello!
      Explain what the phrase "list when x collum value is Yes" means? Is each cell in the column "Yes"?
      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.

  18. Hi, I have a requirement to allow multiple set of numbers in a cell, for example, (00001, 02334, 01289), retaining leading 0s and limit to 5 digits each number, how can I write a formula for this? can someone help me, thanks

    • Hello!

      If I understand your task correctly,
      Set the cell to "Text" format and use Data Validation - Allow - Text length

      If you want to use numbers, set the custom format "00000" in the cell and use the Data Validation formula
      =A1<1000000

      I hope it’ll be helpful.

      • Hi, thanks for getting back on this, the formula and the steps mentioned works for one set of number given on that cell, for example, 00012. My requirement was to enter multiple set of numbers, for example (00012, 11111, 12359, 88888, etc...) which does not work.

        • Hello!
          I wrote this formula based on the description you provided in your original comment. Please note that if you’d provided me with the precise and detailed description of your task from the very beginning, I’d have created a different formula that would have worked for your case. However, even now your question is not precise enough. 00012, 11111, 12359, 88888 written as text in one cell? If so, why are you talking about numbers and leading 0s?

  19. =AND(COUNTIF(Root_Rank,O6)0,O6<=3)

  20. Why might a logically TRUE formula trigger a FALSE in Custom Validation.
    =AND(COUNTIF(Root_Rank,O6)0,O6<=3) is TRUE when O6 is 1, 2, or 3 but fails validation.

    • Sorry formula truncated. =AND(COUNTIF(Root_Rank,O60, O6<=3)

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)