9

I have an Excel spreadsheet where on Sheet 1 the Department Id is represented as a number and where on Sheet 2 there is a list of those numbers with the Department Name next to them.

If possible I would like some help and advice as I want to allow the user to select the Department Name from a drop down list and have it populate a hidden field with the department number.

Something like in Sheet 3 with the Department Id being stored for example in the C column on that page:

Sheet 1

enter image description here

Sheet 2

enter image description here

Sheet 3

enter image description here

4
  • What is "Behind the scenes"? Perhaps a hidden column in the same table that has a vlookup to a second table with your key/value pairs?
    – JNevill
    Oct 23, 2018 at 13:24
  • The idea of having a hidden column is new to me and yes that would work. What I need to do is to have columns like this as the spreadsheet will later be exported to a database which expects numbers in those columns rather than text. Have you seen any examples of something like this that I could use as a starting point?
    – Alan2
    Oct 23, 2018 at 13:33
  • Could you not create a smaller database from the downloaded tables? I'm assuming the master database is something like SQL Server or MySQL - create Access tables with it (maybe using passthrough queries)? Or create a query in the database that will export the descriptions rather than the primary key - then upload to a temporary table and use INSERT & UPDATE queries to normalise the returned data. Oct 23, 2018 at 13:57
  • @DarrenBartrup-Cook - Yes the master database is SQL Server. I was thinking I could hardcode the lookup tables as they rarely change. But not really sure how to link up those look up tables to a drop down that would populate a hidden column containing 1,2, 3 etc in the spreadsheet?
    – Alan2
    Oct 23, 2018 at 14:13

3 Answers 3

11
+100

This could be one approach.

If I have understood you correctly, the user will select the department name in a drop-down list in Sheet 3 and and the index number associated with that department name will appear. The user will not be limited to what name (andy, bob) it is in the adjacent column...

Result will be (notice this is a Excel Table!) :

enter image description here

We have the 3 sheets:

enter image description here

Select the data areas and click on "Format as Table", Select the option "My data has headers". Do this for all the 3 sheets.

enter image description here

Name your tables: "Design" -> "Table Name". I will use "Table1" for "Sheet1", "Table2" for "Sheet2" etc. It will make life easier when you link and refer in formulas. Notice that for "Sheet3" I also add a dummy column C. I name it "Index Number".

enter image description here

Go to "Sheet2" and select the range that you want to use as your drop-down list (in my case i choose Department Names and selected "B2:B8". Click "Formulas" -> "Name Manager" -> "New..."

enter image description here

This window will pop-up. Name it an easy name (I named it: "Department_Names") and check that the "Refers to:" is in this format "=TableX[Column Name]". In our case =Table2[Department]. Notice that the name manager don't use "B2:B8", rather is reference to the table column area. The last step is very important!

enter image description here

Go to Sheet3. Click on cell where you want to apply the drop-down list (I use B2 in the example). Click "Data" -> "Data Validation".

enter image description here

Data Validation window will now pop-up. Go to "List" and in the field "Source:" you write your named range as =Department_Names (the one I named "Department_Names"). The drop-down list will use the values in the range that "Department_Names" are applied to. In our case it will make a drop-down list of all the department names from "Sheet2", range B2:B8.

enter image description here

Go to "Sheet3" and enter the formula below in the cell C2 as shown in the picture.

Notice I use " ; "as delimiter (European version of Excel, US user will probably use" , ").

=IF([@Department]="","",IFERROR(INDEX(Table2[ID],MATCH([@Department],Table2[Department],0)),))

The first part creates our Index number:

...IFERROR(INDEX(Table2[ID],MATCH([@Department],Table2[Department],0)),)
  • Return the Index number from "Table2", Column ID. INDEX(Table2[ID]
  • lookup value in the MATCH formula comes from "Table3", Department (drop-down list)
  • The lookup value will look in "Table2", column Department.

Will take care of 0 that will be created when nothing is selected in the "Table3", Department column (see next picture for example)

=IF([@Department]="","", ...formula...)

enter image description here

Formula without the =IF([@Department]="","", ...formula...)

enter image description here

You can hide the "Index Number" column in "Sheet3" if you don't want to have it visible. Right click on the Column C header and choose "Hide".

enter image description here

Only for demonstration purpose here. I create a dummy column "Only for show hidden column values". So when the user selects a Department name, it will change the index number associated to that department.

enter image description here

So why the use of tables and named range?

Let's say you need to add a new department. We add a department in the "Table2" and we give it ID: 7 and Department Name: Department H.

enter image description here

What happens in your "Sheet3" is that the drop-down list will automatically catch the new row. The range named to "Department_Names" will adjust to the table rows because it's linked to "Table2[Department]". This is also true if you decrease rows for Table2. Source

I have not tested it, but I'm quite sure that you can link a table to an external source. So when you import your data to the table (given the headers is static) the table will be updated with "Refresh All" button in Excel. Something to explore and look further into :)

enter image description here

2
  • 4
    Wow, that's a very impressive answer. I will check that out and let you know if I have any questions. Thanks
    – Alan2
    Oct 28, 2018 at 10:47
  • 4
    You're welcome. Feel free to ask if there are anything that is not clear or doesn't work :)
    – Wizhi
    Oct 28, 2018 at 12:13
5

It can be done with INDEX/MATCH.

As an example:

Sheet1 contains your lookup table.
For simplicity sake Sheet1 has numbers 1 - 6 in the range A1:A6 and A - F in the range B1:B6.
1 - 6 are the primary keys and A - F are the descriptions.

Sheet2 has Data Validation in column A: Allow List using =Sheet1!$B$1:$B$6 as the Source. This will give you the drop-down in column A.
In column B, which is hidden, you have the formula =IFERROR(INDEX(Sheet1!$A:$A,MATCH($A2,Sheet1!$B:$B,0)),"")

The MATCH formula will return the row number in Sheet1 that the description appears in.
The INDEX formula will return a reference to the cell in column A on that same row - giving the Primary Key value.
If no match is found then #N/A is returned which is dealt with by the IFERROR statement.

4

This method will work if your departments are less than equal to 32.

In UTF 8 characters from code 128 to 160 are hidden (among many others). You can use these characters as your id key, you have to just concatenate them with your department name.

This is a screenshot of excel

3rd column is the Unicode character, 4th is concatenation of this character and the department name and last column is extracted id from the new department name.

enter image description here

These are the formulas

enter image description here

At the end it can also work if your departments are more than 32 but you have to find another slot of non-printable characters

You have to use the new department names as data validation drop downs where department entry is required and can extract the id from the entered department

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.