+ Reply to Thread
Results 1 to 6 of 6

Conditional Drop-Down boxes

Hybrid View

Nate Westcott Conditional Drop-Down boxes 11-16-2015, 12:39 PM
GeneralDisarray Re: Conditional Drop-Down... 11-16-2015, 02:58 PM
GeneralDisarray Re: Conditional Drop-Down... 11-16-2015, 03:16 PM
scottc_00 Re: Conditional Drop-Down... 11-16-2015, 04:17 PM
scottc_00 Re: Conditional Drop-Down... 11-16-2015, 04:11 PM
FDibbins Re: Conditional Drop-Down... 11-16-2015, 04:22 PM
  1. #1
    Registered User
    Join Date
    09-30-2011
    Location
    Lithuania
    MS-Off Ver
    Excel 2010
    Posts
    81

    Conditional Drop-Down boxes

    I am wondering if it is possible to write a bit of VBA that would show different drop-down menus based on certain index values selected from other drop-down menus. For instance, in one drop-down, I can select "Fruit" or "Meat". If I select "Fruit", another drop-down menu appears, which offers selections like "Pear", "Orange", and Banana".

    Is this possible? Thank you for any help anyone can offer!
    Nate

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Conditional Drop-Down boxes

    EDIT - found a mistake. Be Right Back.
    Last edited by GeneralDisarray; 11-16-2015 at 03:01 PM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Conditional Drop-Down boxes

    For an example: I used this to allow for State / County selection on a project. This design would work for any situation where your first selection list can be viewed as the 'header row' for a table that list all the options (see attached sheet labeled 'HIDDEN' for my full table).

    Both list, require data validation be used. Also, I put the 'heavy lifting' into the name manager in two named formulas:

    First list: States of the United States, formula is =HIDDEN!$A$1:INDEX(HIDDEN!$1:$1,COUNTA(HIDDEN!$1:$1))

    Second list: to provide Counties for a given state is given by: =INDEX(HIDDEN!$A:$XFD,2,MATCH(VISIBLE!$C$3,HIDDEN!$1:$1,0)):INDEX(HIDDEN!$A:$XFD,COUNTA(INDEX(HIDDEN!$A:$XFD,0,MATCH(VISIBLE!$C$3,HIDDEN!$1:$1,0))),MATCH(VISIBLE!$C$3,HIDDEN!$1:$1,0))


    EDIT:
    If you want the second value to go away when the first value is changed, you need VBA. Attached is a second example that does this (using the following VBA in the Sheet2 ('VISIBLE') module.

    Option Explicit
    
    
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim YES_NO As Integer
        Dim possibleCounties As Range, found As Range
    
        On Error GoTo PROC_ERR
        Application.EnableEvents = False
        
        'State Changed - Clear County Entry IF that county is not part of the new state's list'
        If Not Intersect(Target, Sheet2.Range("C3")) Is Nothing Then
            
        'Check State Name'
            Set found = Sheet1.Range("1:1").Find(WHAT:=Target.Value, LookIn:=xlValues, Lookat:=xlWhole)
            If found Is Nothing Then
                MsgBox ("State Name invalid.  Use pick-list if unsure of spelling." & Chr(13) & Chr(13) & "     Entry, " & Sheet1.Range("B10").Value & ", not found in state list.")
                Sheet2.Range("C3").ClearContents
                Sheet2.Range("C5").ClearContents
                Application.EnableEvents = True
                Exit Sub
                
            ElseIf found = "" Then
                Sheet2.Range("C5").ClearContents
                Application.EnableEvents = True
                Exit Sub
            End If
            
            If found Is Nothing Then
                Sheet2.Range("C5").ClearContents
            Else
                'State was found - check check county.'
                    Set found = found.EntireColumn.Find(WHAT:=Sheet2.Range("C5").Value, LookIn:=xlValues, Lookat:=xlWhole)
                    If found Is Nothing Then Sheet2.Range("C5").ClearContents
            End If
        
        End If '
        
        
        
    PROC_ERR:
            Application.EnableEvents = True
            If Err.Description <> "" Then MsgBox ("ERROR:  " & Err.Description)
    
    End Sub
    Last edited by GeneralDisarray; 11-16-2015 at 03:32 PM.

  4. #4
    Forum Contributor
    Join Date
    01-08-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    100

    Re: Conditional Drop-Down boxes

    One other thing I should mention.
    You should probably use an event procedure to default to the first item in the list of the new Category selected.
    For instance if you choose Meat and Beef, and then change the category to Fruits you dont want Beef to stay.
    So create an event procedure on a change to the first dropdown to change the second dropdown to the first item.

    If you need help on event procedures you can probably find many many post describing how to do it on here in the VBA section.

    Good Luck

  5. #5
    Forum Contributor
    Join Date
    01-08-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    100

    Re: Conditional Drop-Down boxes

    I actually take a different approach when doing this
    I use Data validation as dropdowns

    Dropdown 1 (in cell f2) I would select from a list Meat, Fruits, Vegatables
    Dropdown 2 (in cell F3) has the list of each selection in dropdown 1.

    Dropdown 2 refers to a range (using data validation list) for example A1:A20
    The list of meats may be in B1:B20 Beef, Chicken, Fish, Pork, etc
    Fruits in C1:C20 Pear, Apple, Grapes, etc
    Vegetables in D1:D20 Corn, Lettuce, etc
    in cell A1 use the formula if($F$2 = "Meat",B1, if($F$2="Fruits",C1 etc
    in cell A2 use the formula if($F$2 = "Meat",B2, if($F$2="Fruits",C2 etc

    Hope this helps
    Let me know if you need more details

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: Conditional Drop-Down boxes

    Attached is a non-VBA way to do this, using dependant drop-downs. It uses named ranges for each category and sub-category, and then uses INDIRECT() to find the right range
    Attached Files Attached Files
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 07-21-2013, 07:20 PM
  2. Need help with conditional drop down boxes
    By arogance1 in forum Excel General
    Replies: 1
    Last Post: 07-10-2013, 10:27 AM
  3. Would like to convert drop down boxes to combo boxes
    By valeriej42 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-27-2012, 12:40 PM
  4. Drop down boxes / Combo boxes
    By Colin2011 in forum Excel General
    Replies: 1
    Last Post: 10-09-2011, 10:16 AM
  5. Replies: 10
    Last Post: 11-19-2009, 04:51 PM
  6. Replies: 4
    Last Post: 11-17-2009, 06:16 AM
  7. Drop down boxes
    By CFlack8472 in forum Excel General
    Replies: 7
    Last Post: 10-16-2009, 11:48 AM
  8. drop down boxes
    By mateofeo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-31-2008, 01:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1