Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a filtered, dynamic drop down list

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?

like image 305
user3147973 Avatar asked Dec 31 '13 01:12

user3147973


People also ask

How do you make a drop-down list change depending on selection in Excel?

In the same or in another spreadsheet, select a cell or several cells in which you want your primary drop-down list to appear. Go to the Data tab, click Data Validation and set up a drop-down list based on a named range in the usual way by selecting List under Allow and entering the range name in the Source box.


2 Answers

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

like image 147
teylyn Avatar answered Sep 30 '22 16:09

teylyn


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

like image 27
pnuts Avatar answered Sep 30 '22 15:09

pnuts