Excel Data Validation -- Dependent Lists

Embed Size (px)

Citation preview

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    1/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html

    (http://www.contextures.com/index.html)

    Create Dependent Drop Down ListsLimit the choices in an Excel drop down list, by using named ranges and the INDIRECT function, to create

    dependent data validation lists. For example, select Fruit in column A, and only Fruit items appear in the dropdown list in column B

    Sections

    Video: Dependent Data ValidationIn this dependent data validation example, if a country is selected from the first drop down, only that country's

    cities appear in the next column's drop down list.

     Video: Dependent Data Validation

    Create Named Lists

     Apply the Excel Data Validation

    Test the Excel Data Validation

     Adding a Third Dependent List

    Using Two-Word Items

    Using Items with Illegal Characters

    Using Dynamic Lists

     Video: Block Changes in First Drop Down

    Block Changes in First Drop Down

    Clear Dependent Cell After SelectingDownload the Sample File

    http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    2/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 2

    However, you can use the Excel IF function to make the selection more flexible. If no country is selected, the City 

    column will show a list of world cities in the dependent drop down list.

    Different Drop Down ...

    Create Named ListsStart by creating Named Lists, which will be the choices in the dependent data validation drop down lists. In this

    example, the first list will be named Produce.

    It contains the Produce categories -- Fruit and Vegetable.

    1. Create the first Named Lista. In an empty area of the workbook, type the entries you want to see in the drop-down lists. These should

     be one-word entries, to match the dependent list names that will be created. If you need to use

    multiple word entries, see:

    Using Two Word Items

     b. Select the cells in the list (but not the heading). In this example, cells B3:B4 are selectedc. Click in the Name box, to the left of the formula bar

    d. Type a one-word name for the list, e.g. Produce.

    e. Press the Enter key.

    2. Create the supporting Named Listsa. Type the entries you want to see in the Excel data validation drop-down list for one of the Produce

    categories.

     b. Select the cells in the list.

    c. Click in the Name box, to the left of the formula bar

    d. Type a one-word name for the list, e.g. Fruit. This name must be exactly the same as the matching

    entry in the Produce list.

    http://-/?-https://www.youtube.com/watch?v=J9bSqswWPws

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    3/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 3

    e. Press the Enter key.

    f. Create another list with the items for the next Produce Type -- Vegetable in this example

    Apply the Excel Data ValidationNext, data validation will be applied on the data entry sheet

    The cells in the Product Type column will allow a List.

    The cells in the Item column will use the INDIRECT function to select a list.

    1. Apply the Excel Data Validation

    a. Select the cells in which you want to apply Excel data validation using the Produce list

     b. On the Ribbon, click the Data tab, then click Data Validation.

    c. From the Allow drop-down list, choose Listd. In the Source box, type an equal sign and the list name, for example: =Produce

    e. Click OK.

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    4/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 4

    2. Create the Dependent Data Validationa. Select the cells in which you want to apply dependent data validation using the Fruit or Vegetable List,

    dependent on which Produce Type has been selected

     b. On the Ribbon, click the Data tab, then click Data Validation..

    c. From the Allow drop-down list, choose Listd. In the Source box, type an equal sign and INDIRECT function, referring to the first data cell in the

    Produce Type column:

    In this example, cell C2 is active, so the formula will refer to the Product Type cell in row 2:

    =INDIRECT(B2)

    e. Click OK.

    Note: If cell A2 is empty, you'll see the message shown below. Click Yes to continue.

    Test the Dependent Data Validation1. Click on a cell in the Produce Type column, and select either Fruit or Vegetable, from the Produce drop

    down list.

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    5/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 5

    2. Press the Tab key on your keyboard, to move to the Item column, in the same row.

    3. In the Item column, click the drop down arrow, and select an item. The drop down list shows either Fruit or

     Vegetable items, depending which has been selected in the Produce Type column

    NOTE: If a Produce Type has not been selected, the Item drop down in that row will

    not work.

    Adding a Third Dependent List You could add another set of dependent data validation dropdown lists that depend on the selections in the firsttwo dropdowns. For example, select a country and region, then select a city in that region.

    1. Create the two named ranges and dropdown lists as described above.

    2. Create another set of named ranges, naming them for the available combinations in the first two Excel data

     validation drop-downs. For example, you might create ranges named CanadaOntario and USANewYork.

    3. For the third dropdown, choose to Allow: List, and use a formula that combines the entries in the first two

    columns, and removes the spaces from the names. For example, in cell D2, the data validation formula

     would be:

      =INDIRECT(SUBSTITUTE(B2&C2," ",""))

    Using Two-Word Items You may need to have two-word items in the first Excel data validation drop-down list. For example, your choices

    are 'Red Fruit', 'Green Fruit' and 'Yellow Fruit'

    1. Create the first named range and dropdown list as described above.

    2. Create the supporting named lists, using one-word names, e.g. RedFruit, GreenFruit, YellowFruit

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    6/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 6

    3. For the second dropdown, choose to Allow: List, and use a formula that removes the spaces from the names.

    For example:

      =INDIRECT(SUBSTITUTE(A2," ",""))

    Using Items with Illegal Characters You may need items in the first Excel data validation drop-down list that contain characters not allowed in range

    names, such as the ampersand (&).

    For example, your choices are 'Red Fruit', 'Green Fruit' and 'Yellow & Orange Fruit'. For the dependent lists, you

    can create ranges with one-word names, such as YOFruit. Then, create a lookup table, which lists each item in

    the first Excel data validation drop-down list, and the range where its dependent items will be stored.

    To start, create the item lists and the first Excel data validation drop-down:

    1. Create the first named range and drop-down list as described above. In this example, the range is named

    ProductList, with values in cells A6:A8. The drop-down list in cell A2 uses ProductList as its source.

    2. Create the dependent lists, and name them, using one-word names, with "List" at the end, e.g.

    RedFruitList, GreenFruitList, YOFruitList. In this example, RedFruitList is in A11:A12,

     YOFruitList is in A15:A16 and GreenFruitList is in A19:20.

    3. Select an item from the drop-down list in cell A2.

    Next, you'll create the lookup table, to match each item with its dependent items' range name.

    1. In the column to the right of the FruitList range, enter the code name for each item's dependent list. For

    example, YOFruit is entered as the code name for Yellow & Orange Fruit.

    2. Name the lookup table. In this example, the range A6:B8 is named ProductLookup.

    3. Select cell B2, and from the Data menu, choose Validation.

    4. Choose to Allow: List.

    5. For the Source, enter a formula that uses a VLookup formula to find the dependent list's range name -- a

    combination of the code name and "List".

    For example: =INDIRECT(VLOOKUP(A2,ProductLookup,2,0)&"List")

     With Red Fruit  selected in cell A2, the VLookup formula will return RedFruitList as the range name for the

    dependent list. The RedFruitList items will be displayed in cell B2's drop-down.

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    7/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 7

    To create a 3rd dependent drop down list, use the same techniques, to create lookup tables and items lists.

    In the screen shot below, there are lookup tables named RedFruitLookup, YOFruitLookup and

    GreenFruitLookup. Items lists, with the code names and "List", have been added to the worksheet.

    In the data validation window, use this formula for the 3rd drop down:

    =INDIRECT(VLOOKUP(B2,INDIRECT(VLOOKUP(A2,ProductLookup,2,0)&"Lookup"),2,0)&"List

    The formula finds the lookup table based on the product type selected in cell A2, e.g. RedFruitLook, and gets the

    code for the selected product -- Mac. It adds "List" to the code, and shows the items in the MacList range.

    Using Dynamic Lists

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    8/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 8

    Because the INDIRECT function only works with references, not formulas, the previous method for dependent

    data validation won't work with dynamic lists. Instead, you can use the following method:

    1. Create the first named range and dropdown list as described above.

    2. Create the supporting named lists, and name the first cell in each range, e.g. cell B1 is named Fruit and cell

    C1 is named Vegetables.

    3. Name the column in which each list is located, e.g. column B is named FruitCol and column C is named

     VegetablesCol

    4. For the second dropdown, choose to Allow: List, and use a formula that calculates the lookup range. For

    example, if the first dropdown list is in cell E2:

    =OFFSET(INDIRECT($E2),0,0,COUNTA(INDIRECT(E2&"Col")),1)

    if two-word items will be used, you can include the SUBSTITUTE function in the

    formula:

    =OFFSET(INDIRECT(SUBSTITUTE($F2,"

    ","")),0,0,COUNTA(INDIRECT(SUBSTITUTE($F2," ","")&"Col")),1)

    Video: Block Changes in First Drop DownTo block changes to the first list, you can change the data validation formula, so the list does not appear unless

    the second cell is empty. This video shows you how this technique works, and written instructions are below the

     video.

    Block Changes in Exc...

    Block Changes in First Drop Down

    http://-/?-https://www.youtube.com/watch?v=C2JOhit9hEI

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    9/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 9

     With dependent drop down lists, problems can occur, if someone goes back to the first list, and changes it. Then,

    the first and second selections are mismatched -- for example, in the screen shot below, Vegetable is the first

    choice, and Banana is selected in the second column.

    To block changes to the first list, you can change the data validation formula, so the list doesn't work unless the

    second cell is empty.

    Instead of just referring to the Produce range for the Produce Type drop down, the formula will check for an entry 

    in the Item column.

    If the Item cell is blank, the Produce Type drop down will show the Produce list

    If an item has been selected, the drop down will try to show the range created by the INDIRECT function --

    "FakeRange". Because there is no range with that name, the result is an error, and the drop down will not

     work.

    To change the formula:

    1. Select the data validation cells in the first column

    2. On the Ribbon's Data tab, click Data Validation

    3. For Allow, select List

    4. In the Formula box, type:

    =IF(C2="",Produce,INDIRECT("FakeRange"))

    5. Click OK 

    Now, the drop down in the first column won't work if the Item has been selected in that row.

    Clear Dependent Cell After SelectingIn the previous section, the data validation formula was designed to prevent selections from the first drop down,

    if the cell to the right contained data.

     Another option is to use a macro, to clear the dependent cell, after making a selecting in the first drop down. That

     will prevent mismatched selections.

    In this example, column B contains a drop down list of Regions. After you select a Region, the drop down list in

    column C shows the customers in the selected region.

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    10/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 10

    Go back and select a different region, and the existing customer name will be cleared.

    Add Event Code to Clear the Customer Cell When you select a Region, event code clears the cell to the right. To add the code, right-click the sheet tab, and

    click View Code.

    Paste the following code onto the sheet module. You might need to change the column number, to match the

    column number on your worksheet.

    Private Sub Worksheet_Change(ByVal Target As Range)

    On Error Resume Next

    If Target.Column = 2 Then

    If Target.Validation.Type = 3 Then

    Application.EnableEvents = False

    Target.Offset(0, 1).ClearContents

    End If

    End If

    exitHandler:

    Application.EnableEvents = True

    Exit Sub

    End Sub

    Download the Sample FileDownload a sample file for dependent data validation (DataValDepend.zip)

    Download a sample file for (DataValDepend.zip)dependent data validation with Dynamic Lists

    (DataValDynamic.zip)

    Download a sample file for Clear Dependent Cell After Selecting

    (/datavalidationsamples/datavaldependclear.zip)

     

    http://www.contextures.com/datavalidationsamples/datavaldependclear.ziphttp://www.contextures.com/DataValDynamic.ziphttp://www.contextures.com/DataValDepend.ziphttp://www.contextures.com/DataValDepend.zip

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    11/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 1

    Get All the Excel NewsFor regular Excel news, tips and videos, please sign up for the Contextures Excel newsletter. Your email address

     will never be shared with anyone else.

      Get Excel

    News

    Name:

    Email:

    We respect your email privacy

    (https://www.aweber.com/permission.htm)

    More TutorialsData Validation Basics (xlDataVal01.html)

    Dependent Dropdowns from a Sorted List (xlDataVal13.html)

    Dependent Lists With INDEX (xlDataVal15.html)

    Hide Previously Used Items in a Dropdown List (xlDataVal03.html)

    Use a Data Validation List from Another Workbook (xlDataVal05.html)

    Data Validation Criteria Examples (xlDataVal06.html)

    Data Validation Tips (xlDataVal08.html)

    Data Validation With Combo Box (xlDataVal10.html)

    SearchSearch Contextures Sites

      Search

     __

    Custom Search

      Search

    http://www.contextures.com/xlDataVal10.htmlhttp://www.contextures.com/xlDataVal08.htmlhttp://www.contextures.com/xlDataVal06.htmlhttp://www.contextures.com/xlDataVal05.htmlhttp://www.contextures.com/xlDataVal03.htmlhttp://www.contextures.com/xlDataVal15.htmlhttp://www.contextures.com/xlDataVal13.htmlhttp://www.contextures.com/xlDataVal01.htmlhttps://www.aweber.com/permission.htm

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    12/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 12

    More Tutorials

    Data Validation Basics (xlDataVal01.html)

    Dependent Dropdowns from a Sorted List (xlDataVal13.html)

    Dependent Lists With INDEX (xlDataVal15.html)

    Hide Previously Used Items in a Dropdown List (xlDataVal03.html)

    Use a Data Validation List from Another Workbook (xlDataVal05.html)

    Data Validation Criteria Examples (xlDataVal06.html)

    Data Validation Tips (xlDataVal08.html)

    Data Validation With Combo Box (xlDataVal10.html)

     

    (http://www.contextures.com/datavalidationmultiselectpremium.html)

     

    ContexturesExcel news 

    by email

    Email:

    Name (optional):

    Sign up

    http://www.contextures.com/xlDataVal10.htmlhttp://www.contextures.com/xlDataVal08.htmlhttp://www.contextures.com/xlDataVal06.htmlhttp://www.contextures.com/xlDataVal05.htmlhttp://www.contextures.com/xlDataVal03.htmlhttp://www.contextures.com/xlDataVal15.htmlhttp://www.contextures.com/xlDataVal13.htmlhttp://www.contextures.com/xlDataVal01.html

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    13/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 13

    We respect your email

    privacy

    (http://www.aweber.com

     

    (http://www.contextures.com/exceltoolsaddin.html)

     

    http://www.aweber.com/permission.htm

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    14/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    http://www.contextures.com/xlDataVal02.html 14

    (http://www.contextures.com/datavalidationmultiselectpremium.html)

     

    (http://www.mothresource.com/104-5-1-6.html)

     

    (http://www.contextures.com/datavalidationmultiselectpremium.html)

     

    http://www.mothresource.com/104-5-1-6.html

  • 8/20/2019 Excel Data Validation -- Dependent Lists

    15/15

    1/19/2016 Excel Data Validation -- Dependent Lists

    Last updated: December 30, 2015 2:21 PM

    (http://www.exceluser.com/cmd.php?Clk=2511398)

    Get Excel

    News

    Name:

    Email:

    Copyright © Contextures Inc. 2016

    Privacy Policy (/privacy.html)

    (https://mvp.microsoft.com/en-us/mvp/Debra%20%20Dalgleish-7612)Debra Dalgleish

    (https://mvp.microsoft.com/en-us/mvp/Debra%20%20Dalgleish-7612)

    https://mvp.microsoft.com/en-us/mvp/Debra%20%20Dalgleish-7612https://mvp.microsoft.com/en-us/mvp/Debra%20%20Dalgleish-7612http://www.contextures.com/privacy.html