Forum Discussion

bmckenna44's avatar
bmckenna44
Copper Contributor
Dec 19, 2023
Solved

Data Validation Cascading dropdown lists

I have a spreadsheet that is designed to have a series of cascading pick lists.  First you pick the continent, Country, State, County then finally City.

 

So that all works fine - except for City.  So City is a little different in that it's dependent on both the State and County.  So when I select that Cell - no drop down appears - unlike the other columns,  However if I select that cell and go to Data Validation - and do "Apply" - with no other changes - that Cell then works properly. What. am I missing in my setup that would allow the spreadsheet to work properly without doing the Data Validation;Apply ?  The Wisconsin example in the picture should work properly.

 

Find the sample file at:

 

https://croptrakcom-my.sharepoint.com/personal/pablo_croptrak_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fpablo%5Fcroptrak%5Fcom%2FDocuments%2FExcel%5Fexample&ct=1703000967401&or=Teams%2DHL&ga=1&LOF=1

  • bmckenna44 After additional testing, I was finally able to reproduce the problem on my system. It seems to occur when the lookup_array parameter of the MATCH function (or XMATCH) is constructed directly in the Data Validation source formula. One solution that appears to work is to define a name for the lookup array in Name Manager by concatenating the two lookup columns. For example, define a new name called "state_county" with the following formula:

     

    =choose_city_table[state_name]&"|"&choose_city_table[county_name]

     

    Name Manager: with additional lookup array

     

    Then, modify the Data Validation formula as follows:

     

    =IF(COUNTIFS(state, C2, county, D2)>0, OFFSET(city, XMATCH(C2&"|"&D2, state_county)-1, 0, COUNTIFS(state, C2, county, D2)), city)

     

    Note: I tested this method with Office 2010 (using MATCH), and it worked properly when closing/reopening the file.

     

    If the LET function is available in your version of Excel, you could also simplify the formula as follows:

     

    =LET(h, COUNTIFS(state, C2, county, D2), IF(h>0, OFFSET(city, XMATCH(C2&"|"&D2, state_county)-1, 0, h), city))

     

    Furthermore, instead of using OFFSET with XMATCH and COUNTIFS (as well as IF with ISNUMBER and XMATCH), you could create an XLOOKUP based LAMBDA function in Name Manager called "XLIST" with the following formula:

     

    =LAMBDA(lookup_value,lookup_array,return_array,
       XLOOKUP(lookup_value, lookup_array, return_array, INDEX(return_array, 1)):
       XLOOKUP(lookup_value, lookup_array, return_array, INDEX(return_array, ROWS(return_array)),, -1)
    )

     

    Then, use the XLIST function as the Data Validation source:

     

    =XLIST(C2&"|"&D2, state_county, city)

     

    Note: this method can be used for every level of your dependent dropdown lists.

     

    Please see the attached workbooks for examples of the above-mentioned methods...

9 Replies

  • djclements's avatar
    djclements
    Bronze Contributor

    bmckenna44 Try using XMATCH with multiple criteria as follows:

     

    =IF(ISNUMBER(XMATCH(1, (C2='20409'!P:P)*(D2='20409'!R:R))), OFFSET('20409'!$L$1, XMATCH(1, (C2='20409'!P:P)*(D2='20409'!R:R))-1, 0, COUNTIFS('20409'!P:P, C2, '20409'!R:R, D2)), '20409'!L:L)

     

    For more information on this method, check out: https://exceljet.net/formulas/xmatch-with-multiple-criteria 

     

    Having said that, this method will perform poorly when referencing entire sheet columns. Since the sample file you've provided is using Excel tables, you can improve performance by using structured table references instead. However, Data Validation does not recognize structured table references directly. To get around this problem, you can either define names in Name Manager for the necessary columns in the table, or you can use the INDIRECT function as follows:

     

    =LET(st, INDIRECT("choose_city_table[state_name]"), co, INDIRECT("choose_city_table[county_name]"), ci, INDIRECT("choose_city_table[city]"), IF(ISNUMBER(XMATCH(1,(C2=st)*(D2=co))), OFFSET(ci, XMATCH(1,(C2=st)*(D2=co))-1, 0, COUNTIFS(st, C2, co, D2)), ci))

     

    Note: the above-mentioned formula is 255 characters long, which is the maximum length accepted by Data Validation.

     

    Please see the modified example workbook (attached).

    • bmckenna44's avatar
      bmckenna44
      Copper Contributor
      even using XMATCH, the data validation is not retained after saving and reopening. The formula is still there, but I have to open data validation again and hit okay for it to start working...
      • djclements's avatar
        djclements
        Bronze Contributor

        bmckenna44 That's unfortunate indeed. What version of Excel are you using? Excel for the web? Did you try my modified version of the complete formula? Your original formula contains some logic errors, using AND with ISNUMBER/MATCH on each column separately, then using MATCH with column concatenation for the final output. Plus, there is an error in the OFFSET logic... the row number returned by MATCH needs to be adjusted by -1.

         

        As mentioned in my previous reply, it's highly inefficient to reference entire sheet columns. You'd be better off defining named ranges for each of the applicable table columns in Name Manager. For example, "city", "county" and "state" as show below:

         

        Name Manager: defined names for table columns

         

        Then, use the defined names in the final Data Validation formula as follows:

         

        =IF(ISNUMBER(XMATCH(1, (C2=state)*(D2=county))), OFFSET(city, XMATCH(1, (C2=state)*(D2=county))-1, 0, COUNTIFS(state, C2, county, D2)), city)

         

        Note: your table contains a record where the state, county and city names are blank. As such, the above formula will return an empty string, rather than the entire city list, if both C2 and D2 are blank, because a match was in fact found.

         

        I hope this helps. Best of luck!

  • mathetes's avatar
    mathetes
    Silver Contributor

    bmckenna44 

     

    Is it possible for you to post a link to the same file on OneDrive? I don't have a SharePoint account and wasn't able to get connected via the link you provide.

     

    In the absence of that--since I can't see how you accomplished the "cascade" effect in yours, perhaps this sample file contains a different method.

    • bmckenna44's avatar
      bmckenna44
      Copper Contributor
      https://croptrakcom-my.sharepoint.com/:f:/g/personal/pablo_croptrak_com/Ep3ZFLeHZqVKjUP0I2WicZ8BReKxDvdyebomyURrUG1siQ?e=G9QQ8O
      • mathetes's avatar
        mathetes
        Silver Contributor
        You're over my head with this, and far more sophisticated in the magnitude of your cascades. As you may have noticed, my approach was dependent on a single table with several columns--as many as were needed for the cascading levels. I would think that would work for your need as well, but would certainly be a gigantic table.
        I'd be happy to look more at yours, but would appreciate an explanation of the basic concept behind your IFS function....

Resources