Advanced Data Validation Techniques in Excel [spreadcheats]

Posted on November 25th, 2008 in Learn Excel - 46 comments

Data validation is a great way to keep your users informed about possible values in a cell and guide them to select something appropriate. As part of the spreadcheats series, in this post we will discuss 2 advanced data validation techniques that can help you when you are modeling a complex worksheet.

Problem 1: You have 2 lists of possible values and you want a way to switch between both

PS: Many thanks to Alex who proposed this idea and solution through e-mail.

You have a cell where user can enter any value from 2 lists. But you don’t want to overload the in-cell drop down list with tons of values, and rather prefer a simpler approach like this:

data-validation-switch-lists

Solution: Use an IF() formula in validation criteria

validation-criteria-if-formulaThe solution is to use an if() formula to determine which one of the two ranges should be used to validate cell contents.

  • Select the cell where you want to have this type of validation
  • Go to menu > data > validation
  • In the criteria area, select “allow” as “list”
  • In the source area, specify a formula like this: =IF($B$7="Full List",Full-list-range,Partial-list-range)

That is all, you now have a data validation list that can change its source based on user preference.

Problem 2 : You would like to change a list’s values based on what is selected in another list

data-validation-change-listsPS: Many thanks to Catherine for asking this question through email

You have a status tracking spreadsheet where each employee enters the status for each of the projects they are working on. They enter the status by first selecting the department and then selecting a project (from that department).

So how do you do this in Excel?

Solution: Use OFFSET and MATCH to determine which range to use

Remember the offset() and match() formulas we discussed in the last spreadcheats?  Assuming the list of projects for each department is in a range B10:C22 with column B having the department name and column C having the project name and the list is sorted on column B, we can use offset() and match() combination along with countif (ahem!) to determine which range to use for project cell drop-down.

  • For the department cell, we can use simple list validation with values as “Marketing, Ops, Sales, IT”
  • For project cell, go to data validation (menu > data > validation) and specify a formula like this:
    =OFFSET(C9,MATCH($B$6,$B$10:$B$22,0),0,COUNTIF(B10:B22,$B$6),1)
  • What is above formula doing? It is fetching a sub-range from the by finding where the first entry for the selected department is, returning x number of rows from that point, where x = no. of projects in that department.

That is all. You now have a list drop-down that changes values based on what is selected in an earlier cell.

Still having doubts?

Feel free to download this example workbook containing a tutorial on Advanced Data Validation in Excel and poke around to learn.

Subscribe to PHD to become supergood in day to day Excel in 30 days

Our spreadcheats series of posts will help you become good & productive in day to day MS Excel usage. Please subscribe to my RSS feed to get fresh new posts on excel

46 Responses to “Advanced Data Validation Techniques in Excel [spreadcheats]”

  1. Jorge Camoes says:

    Let me add that usually you should have those lists in a “control” or “support” sheet, creating a named range for each list. Then you could enter: If($B$7=”Full List”, range1, range2). Just a bit cleaner.

  2. Chandoo says:

    @Jorge: You are right… using a control sheet is always advisable.

    • AFP says:

      =OFFSET(C9,MATCH($B$6,$B$10:$B$22,0),0,COUNTIF(B10:B22,$B$6),1)

      OFFSET(reference, rows, cols, [height], [width])

      Hello everybody,

      I am trying to use the OFFSET proposed formula =OFFSET(C9,MATCH($B$6,$B$10:$B$22,0),0,COUNTIF(B10:B22,$B$6),1) but doing “reference” from a another sheet in the same woorkbook.

      Can you confirm that the OFFSET accept a “reference” from another sheet in the same workbook, e.g. sheet2!C9…

      Thank you in advance for your feedback.

      • Hui... says:

        @AFP
        Yes, That can be done and your format is correct
        =OFFSET(Sheet2!C9,MATCH($B$6,$B$10:$B$22,0),0,COUNTIF(B10:B22,$B$6),1)

        Just remember that if there are multiple values of the cell B6 in the range B10:B22 that you will get a #Value! error
        That occurs as you are trying to return a Range which will be the No of occurrences of B6 in the Range B10:B22 long
        If you want just the cell at the offset, use:
        =OFFSET(Sheet2!C9,MATCH($B$6,$B$10:$B$22,0),0,1,1)

        If you want to sum up that many cells use
        =Sum(OFFSET(Sheet2!C9,MATCH($B$6,$B$10:$B$22,0),0,COUNTIF(B10:B22,$B$6),1))

  3. Ketan says:

    @ Jorge & Chandoo == Thanx for nice idea. Without naming the range, control/list from other sheet does not work.

  4. Ketan says:

    @ Jorge & Chandoo == Ref. Problem#2
    If you make databse in following order of Area, it won’t work :
    Marketing
    Ops
    Marketing
    Sales
    Sales
    Marketing

    i.e. all the similar areas are to have one after other OR need to sort on area.

    Your comment pls !

  5. Lincoln says:

    @Ketan

    That’s why it says in the solution that “the list is sorted on column B”.

  6. Chandoo says:

    @Lincoln: thanks…

    @Ketan: you are right The list needs to be sorted as I have noted in the article.

    There are some solutions involving array formulas (shudder) etc. to overcome this, but I always try to keep these things simple so that anyone can understand and use. As such I am no good at array formulas myself and don’t venture in to them unless they are the only option.

  7. Cheryl says:

    Since we are on the topic of named ranges (well sort of) can someone tell me an easy way to rename a named range? I have a report where it would be really helpful to use a named range in my vlookup but the range varies month to month..
    And since it’s Thanksgiving, I wanted to say thanks to you Chandoo because people think I spend hours & hours researching how to do things when really most of my information comes from right here!! :)

  8. azmat says:

    I have a question….may b m asking for too much….can it work like we have on web pages…i wud illustrate it wid an example as to what actualy am lukin for….

    suppose when we select deptt “Ops” then it should remove the value currently present in project value instantly…….(if it’s not of “Ops” deptt)

  9. Chandoo says:

    @Cheryl… thank you. did you try using indirect or offset functions in the named range definition. That way even though the names stays same, you can change the range it refers to by simply changing value in a control cell. Let me know if you have trouble in doing this. I can elaborate on this.

    @Azmat: hmm.. resetting value on previous selection… I guess you can use VBA to get this effect. But you wouldnt probably want to use vba. I dont know other ways around this. Does any one know how to reset a data validation enabled field when some other cell changes?

  10. Cheryl says:

    I have not, but I know you did have a posting about those recently. I will check that out! Thank you.

  11. Karthik says:

    Have a simpler version of this solutions.
    1. Define a name range with name as “Department” and list containing “Marketing, Ops, Sales, IT”
    For Ex: In D1 put the title as Department, D2 as Marketing, D3 as Ops and so on
    2. For each of the projects define a named range with the department names.
    E1 will have the title Manufacturing, E2 has project 1,project2 …
    3. In Cell A1 use data >> Validation >> source = Department
    4. In Cell B2 just use data >> Validation >> source =INDIRECT(A1)

  12. Asif says:

    Hey in all the above examples the ‘list’ is in the same Excel file. What will I do if my ‘list’ is another Excel file?

  13. [...] Check out Chandoo’s blog – Pointy Haired Dilbert – article ‘Advanced Data Validation’ [...]

  14. Darwin de Leon says:

    Why is it that if you extend the Range ($B$10:$B$22) to for example ($B$10:$B$33) it gives you wrong output?

  15. Chandoo says:

    @Darwin .. Are you sure you have edited the range in all places?

  16. Darwin de Leon says:

    I used your sample Excel file to test the data and extend the rows up to B33, then in Data Validation>Source I try to change the formula to this: =OFFSET(C9,MATCH($B$6,$B$10:$B$33,0),0,COUNTIF(B10:B33,$B$6),1)

    If I select Marketing, the resulting list includes Projects under Ops Area.

  17. Darwin de Leon says:

    I sent you an email with your sample data and additional rows in your gmail.

  18. Chandoo says:

    @Darwin… Did you sort the list by department name? It seems to work fine for me.

  19. Darwin de Leon says:

    Got it! I should have paid attention to the comments here. Thanks!

  20. Adnan Rafiq says:

    Can anybody tell me about calender of date option in data validation, when I wish to put any date then calender should appear on screen.

    Your reply will be highly appreciated.

    thanks and and regards,
    Adnan

  21. Chandoo says:

    @Adnan… I think you have to use a bit of VBA to show calendar control to let user enter valid dates.

  22. Amien says:

    I’ve got a few columns of data where the next column need to refer to the previous as data vaildation eg. District, Area, Area Manager, Project No. etc. if i choose a district (North) it brings up only the Areas for North but how can I go beyond that so that in a next cell I can select the Area Manager for that Area & the Project numbers for that Area

  23. Venkatesh says:

    Dear Chandoo,

    Am a Silent reader of your posts and this blog..Its quite interesting and very useful for me..
    In the above post, using Indirect(Cell reference) also works very well…The referenced cell may contain any one of the Name..

    Venkat

  24. Hui... says:

    @Adnan
    There are several examples and free pop up calendars
    a quick search of Google will find references to both
    http://www.google.com.au/search?hl=en&safe=off&q=popup+calendar+in+excel&btnG=Search&meta=&aq=f&oq=

  25. Venkatesh says:

    Amein, the same can be done with the use of Name Manager and INDIRECT function…

  26. Jon says:

    This is excellent – really helped me out. However, still a little stuck I’m afraid. I have 7 lists in total, each needs to feed off the preceeding list. Can you give me some guidance on what I need to have in the data validation cells from list 3 – 7 so it includes all of the previous entries?

    Additionally, there is some overlap in each list (i.e. I have one list titled Region, where Global is seen in more than one of the preceeding categories). Right now, when I click the drop down list I get multiple Global’s rather than just one.

    Any thoughts? I’ve been working on this for ages now so any help you can give would be great!

  27. Francis says:

    Chandoo,

    What if I have problem 1 in problem 2 above?

    To explain, If my list under marketing is too big and I only want to see only the items that are frequently used instead of all available items, how should I amend my formula?

    Help please!!!

  28. john says:

    The data has to be in the same worksheet i believe? usually a separate worksheet is used for reference. could you clarify how do you go about it? It doesnt work if you reference the data from a different sheet. the control page or something mentioned is not clear to me.

  29. ahmet says:

    Hi Chandoo,

    I am not too sure if i can use data validation with the problem that i have.

    I have a data enry of around 5000 rows in 1 sheet and i want to be able to select a person randomly after applying auto filters.

    i am not sure if this is possible.

    say for example, i want to select a name randomly which is in coumn D after filtering using other columns which shows me who is attended a specific course, meeting etc.

    your help would be much appreciated.

    Thanks

    Ahmet

  30. Chaluva says:

    Hi,
    Please help in creating Data Validation which accepts only text.

    Please Note: It should not accept text and number combination.

    Thanks
    Swamy

  31. Ananthanarayanan says:

    I have created a file with data validation. In a column there are over 75 entries in a drop down data. One has to scroll through the list to select an entry.
    Is there a way by which just by entering the 1st letter or the 1st two letter, the drop down list shownall the data with these starting letters so that the selection becomes easier.
    My thanks in advance for this guidance.

  32. Hui... says:

    @Ananthanarayanan
    Excel doesn’t have an Auto Complete facility in Data Validation.
    But with a bit of careful planning you can achieve the same result.
    Read how here:
    http://www.ozgrid.com/Excel/autocomplete-validation.htm

  33. jagmohan says:

    Question:

    If the lists are as follows what is the expected list in first dropdown?

    B C
    a aa
    a ab
    a ac
    a ad
    b ba
    b bb
    c ca
    c cb
    c cc
    c cd
    c ce
    d da
    d db
    d dc

    What I get in first dropdown is {a, a, a, a, b, b, c, c, c, c, c, d, d, d}. How to reduce it down to only {a, b, c, d}

    regards,

    Jagmohan

  34. Hui... says:

    @Jagmohan
    Have a look at some techniques here
    http://chandoo.org/wp/2010/02/02/data-validation-using-an-unsorted-column-with-duplicate-entries-as-a-source-list/
    .
    http://chandoo.org/wp/tag/unique-items/
    .
    or use an Advanced Filter
    .
    All of these techniques can be automated if required.

  35. jagmohan says:

    Hi Hui,

    Thanks. I tried to use the Pivot table technique and it worked. Further question on the same.

    Now I add few items in column H and column I (sorted again) – as follows

    Excel Information
    The lists are in Column H and I starting from row 4. F9 contains the first drop down. G9 contains the second dropdown. Pivot is located in Column M starting from row 13.

    H I
    a aa
    a ab
    a ac
    a ad
    b ba
    b bb
    c ca
    c cb
    c cc
    c cd
    c ce
    d da <- New item
    d db <- New item
    d dc <- New item

    Now is it that I need to delete the pivot table, create a new one and then use it? Or is there any technique by which I can dynamically grow the pivot table?

    Ofcourse I changed the formula in G9 to following
    offset(I4, match($F$9, offset($H:$H,0,0,counta($H:$H)-1,1),0), 0, countif(offset($H:$H,0,0,counta($H:$H)-1,1),$F$9), 1)

  36. Hui... says:

    @Jagmohan

    If the pivot table is based on a Dynamic Range, then refreshing the pivot table will add the extra items.

  37. jagmohan says:

    Now how do I check that?

  38. Hui... says:

    @Jagmohan
    Dynamic Ranges expand/defalte as the data in the range increases or is deleted
    They are added using Named Formula
    refer: http://chandoo.org/wp/2009/10/15/dynamic-chart-data-series/
    Once you setup a Named Formula for your range, change the pivot table to be based on that range
    As you add or remove data and update the pivot table it will adjust for the new data scenario

  39. vamique says:

    how do i do the same if there are more than 2 columns for eg , country, state, city.

  40. Bruce says:

    I want to be able to use data validation where the restricted input would be either a number greater than 0 or the text “na”. I tried using an OR statement, but it didn’t work. Not sure if I just had bad syntax or if I can’t do it that way. Any help would be appreciated.

  41. baum schausberger says:

    how to generate a list of non-repeating combinations, with some values sums off, and some values on, beside show how many evens and odds numbers.

  42. Tanvir Khan says:

    Thanks Chandoo, it’s really very helfull formula. But the formula doesn’t work if we copy and paste in the other rows and we can correct by taking out $ from $B$6. So the correct formula should be

    =OFFSET(C9,MATCH($B6,$B$10:$B$22,0),0,COUNTIF(B10:B22,$B6),1)

Leave a Reply