+ Reply to Thread
Results 1 to 7 of 7

Dynamic/dependent data validation lists - reset second cell if first cell changed

Hybrid View

gazza365 Dynamic/dependent data... 12-15-2010, 09:53 PM
teylyn Re: Dynamic/dependent data... 12-16-2010, 05:21 AM
JBeaucaire Re: Dynamic/dependent data... 12-16-2010, 05:37 AM
teylyn Re: Dynamic/dependent data... 12-16-2010, 05:46 AM
gazza365 Re: Dynamic/dependent data... 12-16-2010, 05:14 PM
mart23mj Updating Monthly Data Chart... 12-16-2010, 05:35 PM
JBeaucaire Re: Dynamic/dependent data... 12-16-2010, 06:59 PM
  1. #1
    Registered User
    Join Date
    11-09-2010
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    32

    Dynamic/dependent data validation lists - reset second cell if first cell changed

    Hi all and thankyou in advance,

    I have a sheet with drop-down list validations in cells A1 and B1 (reading off named lists from another sheet).
    The contents of B1 is dependent on what is selected in A1.

    For example:
    - if in A1 "Cities" is selected, then the drop-down in B1 is a list of different cities.
    - if in A1 "Countries" is selected, then B1 is a list of different countries.

    I want to make sure that B1 reverts to blank if A1 is changed. I also want to be able to insert a row underneath Row 1 and have the validations copy down.

    So far I've managed to make B1 go blank whenever A1 is changed using the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim strName As String
        If Target.Cells.Count > 1 Then Exit Sub
            On Error Resume Next
            strName = Target.Name.Name
            On Error GoTo 0
            
        If strName = "Pick_a_City" Then
           Application.EnableEvents = False
           Range("Corresponding_List") = vbNullString
           Application.EnableEvents = True
        End If
    End Sub
    In the above, "Pick_a_City" is a named range referring to cell A1 and "Corresponding_List" refers to cell B1.

    The problem is that this code relies on named ranges so that when a new row is inserted, the drop-down boxes copy (to A2 and B2) but B2 does not go blank if A2 is changed.

    Is there a way to do what the above code is doing without needing to refer to named ranges. Maybe something to do with activecells and offsets? I've been battling this for a while but I'm new to VBA and haven't been able to crack it.

    I've attached a simplified file as an example. I'm using Excel 2007.

    Any help greatly appreciated.

    Thanks
    G
    Attached Files Attached Files
    Last edited by gazza365; 12-16-2010 at 07:00 PM. Reason: added code tags for newbie

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Dynamic/dependent data validation lists - reset second cell if first cell changed

    Hello,

    Please wrap all code in code tags. The forum rules explain why and how. I'll do it for you this time, but this time only.

    So far I've managed to make B1 go blank whenever A1 is changed using the following code:
    I don't think your code does that at all. At least not in my trials.

    Your code to insert a new row already duplicates the data validation formulas, so all you need is to clear cell Bx when cell Ax is changed. Code to do this could go like this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        On Error Resume Next
        Application.EnableEvents = False
        Target.Offset(0, 1).ClearContents
        Application.EnableEvents = True
    End If
    End Sub
    cheers

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Dynamic/dependent data validation lists - reset second cell if first cell changed

    The only thing I would add to Teylyn's great improvement to your macro is to not abort if you changed multiple cells at once in column A:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell as Range
    Application.EnableEvents = False
    
    For Each cell in Target
        If Not Intersect(cell, Range("A:A")) Is Nothing Then _
            cell.Offset(0, 1).ClearContents
    Next cell
    
    Application.EnableEvents = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Dynamic/dependent data validation lists - reset second cell if first cell changed

    Edit: just posted utter nonsense. Time to call it a day.

    thanks for the improvement, Jerry.
    Last edited by teylyn; 12-16-2010 at 05:49 AM.

  5. #5
    Registered User
    Join Date
    11-09-2010
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Dynamic/dependent data validation lists - reset second cell if first cell changed

    Great, thanks for the help all (and sorry for not following the guidelines - as you can probably tell I'm new to all of this!)

  6. #6
    Registered User
    Join Date
    12-16-2010
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    2

    Updating Monthly Data Chart From Extract Tab

    test test test
    Last edited by mart23mj; 12-16-2010 at 05:51 PM.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Dynamic/dependent data validation lists - reset second cell if first cell changed

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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