Generate a filtered, dynamic drop down list
Asked Answered
P

5

6

I need two dynamic drop down lists for data validation. One containing a unique list of continents to choose from, and then the second list which is a dynamically generated subset of countries based on the continent selected. The data is not in any particular order:

     A          B
---+--------------------
1  | Continent  Country
2  | Africa     Algeria
3  | Asia       China
4  | Africa     Ethiopia
5  | Europe     France
6  | Europe     Germany
7  | Asia       India
8  | Europe     Italy
9  | Asia       Japan
10 | Europe     Poland
11 | Africa     South Africa
12 | Europe     Spain

I have successfully created the first drop down list by using a hidden column to generate a unique list of continents and then associate them as a named range. So that part's done, however

how do I create a second dynamically generated, filtered list (preferably without any gaps in the list) based on the Continent association selected in the first list?

The actual data I'm digesting is thousands of data points large, so performance is a concern, and I'd prefer to not use VBA if possible.

Edit: With a bit more searching I found a link that was helpful, that provided me with this formula: IFERROR(INDEX($A$2:$A$100,SMALL(IF($B$2:$B$100="Yes",ROW($A$2:$A$100)-ROW($A$2)+1),ROWS($A$2:$A2))),"")

It's closer, however it won't work since I'd need to put these in a separate column in my worksheet for every row where I need the dynamic drop down list, plus I'm unsure how large the filtered list will be.

Is there any way of doing this directly inside a named range?

Patois answered 31/12, 2013 at 1:5 Comment(2)
Have a look at this tutorialSubalpine
Thanks, that tutorial clearly describes the dynamic drop down part, but it assumes that all the lists and sub-lists are already organized. I have a mish mash of data in two columns, that I have to dynamically filter out and make those sub-lists first. This is what I'm having the most trouble with.Patois
M
2

If you are prepared to apply a series of named ranges (in the example, the occupied portions of B:G) then Data Validation with Allow: List and Source: =INDIRECT(INDEX($1:$1,MATCH(I2,A$1:A$7,0))) might suit for entries in Column J:

SO20850122 example

Attempt at clarification:

It is generally easier to understand a formula from the inside outwards, so to start with =MATCH: this looks to find the position in the A1:A7 list of the exact (ie ,0) value entered in I2. For S.AMERICA - good choice! :-) - that happens to be last (7th). This result is then fed to the =INDEX part as the parameter to determine which column number is the relevant one (for the Validation by country). For Excel ColumnG is the seventh.

I have not been entirely conventional with the formula by not bothering with an array that has more than a single row/column – in part for the sake of convention that Validation and Lookup lists are more often arranged vertically than horizontally (eg VLOOKUP applied more often than HLOOKUP), in part because we are more used to seeing labels for columns than we are for rows, etc. – in effect for the same reason that spreadsheets are generally allowed to be longer than they are wide (eg 16,384 columns but 1,048,576 rows), in part for the sake of the image aspect ratio in my answer.

Alternative layouts, though possibly slightly less convenient in practice (eg for setting up the named ranges) might nevertheless help with my attempt at explanation:

Left Image below: Transpose the country data to a more conventional layout for a vlookup (and might as well then delete row with Continents) and the validation formula would be:

=INDIRECT(INDEX(A$1:A$6,MATCH(A8,A$1:A$6,0)))  

(for which I had to move the ‘trigger cell’ I2 etc as that cell is now required for other data – I chose A8 etc instead).

Right Image below: Leaving ‘trigger cell’ as for Left image and also doing away with Continents, the validation formula would be:

=INDIRECT(INDEX(C$1:H$1,MATCH(A8,C$1:H$1,0)))

SO20850122 second example

Marchant answered 31/12, 2013 at 22:45 Comment(3)
I'm attempting to implement your solution, but I'm confused, why INDEX($1:$1, ? how does that reference the list in B,C, etc?Patois
Thanks for the effort, I just wasn't familiar with the "1:1" formatting with Excel. I ended up using a modification of your implementation.Patois
A extensive explanation of this approach can also be found here: contextures.com/exceldatavaldependindextables.htmlLangton
S
3

If you sort the list ascending by continent, then you can build a dynamic range name without any helper cells, with the formula

=INDEX(Sheet1!$B:$B,MATCH(Sheet1!$E$2,Sheet1!$A:$A,0)):INDEX(Sheet1!$B:$B,MATCH(Sheet1!$E$2,Sheet1!$A:$A,1))

where cell E2 has the selected continent. Here is a screenshot with the same scenario. Replace Category with Continent and replace SubCategory with Country.

enter image description here

Sectary answered 1/1, 2014 at 2:1 Comment(1)
This is a good alternative not requiring so many cells with with formulas, however my data set can't have the restriction of having the data sorted.Patois
M
2

If you are prepared to apply a series of named ranges (in the example, the occupied portions of B:G) then Data Validation with Allow: List and Source: =INDIRECT(INDEX($1:$1,MATCH(I2,A$1:A$7,0))) might suit for entries in Column J:

SO20850122 example

Attempt at clarification:

It is generally easier to understand a formula from the inside outwards, so to start with =MATCH: this looks to find the position in the A1:A7 list of the exact (ie ,0) value entered in I2. For S.AMERICA - good choice! :-) - that happens to be last (7th). This result is then fed to the =INDEX part as the parameter to determine which column number is the relevant one (for the Validation by country). For Excel ColumnG is the seventh.

I have not been entirely conventional with the formula by not bothering with an array that has more than a single row/column – in part for the sake of convention that Validation and Lookup lists are more often arranged vertically than horizontally (eg VLOOKUP applied more often than HLOOKUP), in part because we are more used to seeing labels for columns than we are for rows, etc. – in effect for the same reason that spreadsheets are generally allowed to be longer than they are wide (eg 16,384 columns but 1,048,576 rows), in part for the sake of the image aspect ratio in my answer.

Alternative layouts, though possibly slightly less convenient in practice (eg for setting up the named ranges) might nevertheless help with my attempt at explanation:

Left Image below: Transpose the country data to a more conventional layout for a vlookup (and might as well then delete row with Continents) and the validation formula would be:

=INDIRECT(INDEX(A$1:A$6,MATCH(A8,A$1:A$6,0)))  

(for which I had to move the ‘trigger cell’ I2 etc as that cell is now required for other data – I chose A8 etc instead).

Right Image below: Leaving ‘trigger cell’ as for Left image and also doing away with Continents, the validation formula would be:

=INDIRECT(INDEX(C$1:H$1,MATCH(A8,C$1:H$1,0)))

SO20850122 second example

Marchant answered 31/12, 2013 at 22:45 Comment(3)
I'm attempting to implement your solution, but I'm confused, why INDEX($1:$1, ? how does that reference the list in B,C, etc?Patois
Thanks for the effort, I just wasn't familiar with the "1:1" formatting with Excel. I ended up using a modification of your implementation.Patois
A extensive explanation of this approach can also be found here: contextures.com/exceldatavaldependindextables.htmlLangton
D
0

Ok. Then starting from creating a dynamic second list, you could go this easy way with some additional fields and columns:

In E1 I started with the selected continent. This has to refer to the first dropdown or E1 can be replace in the following formulas.

In C2 you start an helping column with the formula =IF(A2=$E$1;MAX($C$1:C1)+1;"") and drag it down to the end of your country list.

Not necessary but easier to understand, if you put =MAX(C:C) in E2 to keep the number of countries for this continent somewhere.

From D8 I start writing some numbers (1, 2, ...) down maybe until D50 and then I write =IF(D6<=$E$2;INDEX(B:B;MATCH(D6;C:C;0));"") in E8 and drag it down to E50.

At the end looks something like this for me, where the range F8 maybe to F50 is named "countryDropdown", which can be used with =INDIRECT("countryDropdown") in the data validation.

     A          B              C     D          E
---+------------------------------------------------------------
1  | Continent  Country              Selected   Africa    
2  | Africa     Algeria        1     Max Number 3
3  | Asia       China         
4  | Africa     Ethiopia       2     
5  | Europe     France               
6  | Europe     Germany              
7  | Asia       India                
8  | Europe     Italy                1          Algeria
9  | Asia       Japan                2          Ethiopia
10 | Europe     Poland               3          South Africa
11 | Africa     South Africa   3     4
12 | Europe     Spain                5
Dent answered 31/12, 2013 at 4:58 Comment(1)
This solution seems to be the simplest, however the purpose of this exercise is to make the dynamic drop-downs available per row, in another table. I fear that it won't work - unless there's someway of saying "oh now use this cell for the continent source, then onClick this one instead"?Patois
B
0

Please check my approach for this:

  1. Data need not be sorted
  2. Supports Many to Many relation
  3. No VBA
  4. No named ranges
  5. Don't need to know unique items names or amount beforehand

OPEN IMAGES IN NEW TAB TO SEE BETTER:

In Sheet1 (The Data sheet): enter image description here
As formulas: enter image description here

In Sheet2 (The helper sheet): enter image description here
As formulas:
enter image description here

In Sheet3 (The result sheet): enter image description here

Butler answered 16/5, 2015 at 15:26 Comment(0)
A
0

I guess I am a gravedigger now but I was looking for the same thing and found this thread. All the different options were so hard to do and understand but I developed my own solution.

All it takes is: 6 formulas in total. One support column and one support sheet.

Here it is:

I based it on this table set up with continents and countries, from the original post. It has Continents and Countries in columns and is named "Countries". It is the table that holds all Continents and its respective countries. It is in Sheet1.

I then made a new sheet (Sheet2) with a new table ("Test") which had the following columns: Continent (to hold a drop down of the unique continents), Country (to hold a dropdown with the countries that belong to the continent chosen) and a support column (Order) that adds the order of the continent, in the unique list of continents. The formula of the support column is:

=MATCH([@Continent],UNIQUE(Countries[Continent]),0)

How the table "Test" looks now. It has 3 columns: Continent, Country and Order

Then I create a new sheet (Sheet3). In A1 I add this formula, to get all the unique Continents in a column: =UNIQUE(Countries[Continent])

In B1 I add the following formula: =IFERROR(TRANSPOSE(SORT(INDEX(Countries[Country],SMALL(IF(Countries[Continent]=$A1,ROW(Countries[Continent])-1),ROW(INDIRECT("1:"&COUNT(IF(Countries[Continent]=$A1,ROW(Countries[Continent])-1)))))))),"")

It finds all countries in table "Countries", that have the continent that shows up in column A of Sheet2 and fill them into the columns, in alphabetical order. I expand this formula into all rows of column B (or just as many as the number of choices that you need, with continents, 9 should suffice).

How it should look when sheet3 is set up.

When that is done I make a named range for all the continents. I name it "UniqueContinents" =INDIRECT("Sheet3!$A$1:$A$"&COUNTIF(Sheet3!$A:$A,"*"))

Now all that is left is to add the data validation.

In table "Test" in the column "Continent" I add the data validation: =UniqueContinents

In table "Test" in the column "Country" I add the data validation: =INDIRECT("Sheet3!"&ADDRESS($C2,2)&":"&ADDRESS($C2,COUNTIF(INDIRECT("Sheet3!"&$C2&":"&$C2),"*"))) It fetches the columns from Sheet3 on the row that matches the chosen Continent.

And I am done. If I add more Continents or Countries into the "Countries" table, all other dropdowns grow dynamically. If I delete something, it gets deleted everywhere.

This has been translated from the Swedish formulas so if something does not work properly, check that I have not missed the conversion. We use ; and not , as separators in formulas, for example.

The table with a working dropdown.

Ardoin answered 3/2, 2022 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.