+ Reply to Thread
Results 1 to 10 of 10

Thread: automatically change selected values in column Y when values in Column U change

  1. #1
    Registered User
    Join Date
    10-31-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    automatically change selected values in column Y when values in Column U change

    Problem:
    Using Excel 2007, how I can automatically change selected values in column Y when values in Column U change? For example (using example data from another user):

    Cell U2 is a validation list with options: Boat/Car

    Cell Y2 is a validation list where if U2 = Boat the options are Catamaran/Sailboat/Tugboat. If U2 = Car the options are Convertible/Sedan/Midsize.

    The problem is that if Y2 is preselected as Sailboat, for example, and you go back and change U2 to equal Car, cell Y2 remains as Sailboat - an option from the Boat validation list. I would like it to automatically update to being "-" as the 1st item on the car validation list. I also need it to happen for all cells in the U and Y columns.
    So starting values of sheet are:
    U2 = Boat Y2 = Sailboat
    U3 = Car Y3 = Sedan
    U4 = Boat Y4 = Catamaran


    If I change U2 = Car, I want Y2 = "-" (which is the first option in the Car validation list.
    If I change U3 = Boat, I want Y3 = "-" (which is the first option in the Boat validation list.
    If I change U4 = Car, I want Y4 = "-" (which is the first option in the Car validation list.
    and so on ... for the cells in columns U and Y.

    I have tried to use the solution posted by lenze as follows:

    Private Sub WorkSheet_Change(ByVal Target as Range)
    If Target.Address = "$U$2" Then Range("$Y$2").ClearContents
    End Sub

    ...but although this works for the first row with the cells of U2 and Y2, I can't get it to work for the rest of the cells even though I have tried dragging the cell Y2 down the Y column and tried using the format Painter button down the Y column.

    Can anyone help?

    Thanks
    RainingAgain
    Last edited by RainingAgain; 10-31-2011 at 01:12 PM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,213

    Re: automatically change selected values in column Y when values in Column U change

    One way:
    Private Sub WorkSheet_Change(ByVal Target as Range)
    Dim Cell As Range
    
    Application.EnableEvents = False
    For Each Cell In Target
        If cell.Column = 21 Then Cells(cell.Row, 25).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!)

  3. #3
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,187

    Re: automatically change selected values in column Y when values in Column U change

    Try:

    Private Sub WorkSheet_Change(ByVal Target As Range)
    If Target.Row = 1 Then Exit Sub
    If Target.Column = "$U" Then
        Application.EnableEvents = False
        Target.Offset(0, 4).ClearContents
        Application.EnableEvents = True
    End If
    End Sub

    Please note that, according to forum rules, any code examples should be enclosed in Code Tags.

    Regards

  4. #4
    Registered User
    Join Date
    10-31-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: automatically change selected values in column Y when values in Column U change

    Very sorry for not putting in Code Tags - first time using any forum and don't know how to put in code tags - sorry again. I am trying to fiollow the forum rules.

    Thansk for you replies, I'll give them a try.

  5. #5
    Registered User
    Join Date
    10-31-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: automatically change selected values in column Y when values in Column U change

    I'll give this a try as well as the suggestion by TM Shucks.


    Thanks

  6. #6
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,187

    Re: automatically change selected values in column Y when values in Column U change

    This:

    If Target.Column = "$U" Then

    might need to be changed to:

    If Target.Column = 21 Then

    Apologies, TMS

  7. #7
    Registered User
    Join Date
    10-31-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: automatically change selected values in column Y when values in Column U change

    Just tried the code by J Beaucaire and that has worked. To be honest, I cannot understand how it works, but I am really grateful for the time and replies from TMs and JB.

    I am not sure if I am replying to you both (TMS and JB) or just you, TMS, as instructions for using the Forum a bit beyond me, but I am trying to do things right!

    Thanks again.

  8. #8
    Registered User
    Join Date
    10-31-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: automatically change selected values in column Y when values in Column U change

    Thanks your suggestion has worked beautifully - thanks.
    I don't fully understand how it has worked, but it has, so I am really heppy.

    Thanks again.

  9. #9
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,187

    Re: automatically change selected values in column Y when values in Column U change

    To target an individual, it is common practice to use "@", for example, @TMS.

    My version

    Private Sub WorkSheet_Change(ByVal Target As Range)
    If Target.Row = 1 Then Exit Sub
    If Target.Column = 21 Then
        Application.EnableEvents = False
        Target.Offset(0, 4).ClearContents
        Application.EnableEvents = True
    End If
    End Sub

    The first line defines the Change Event which is passed a "Target" range; this may be one or more cells, rows or columns. You can refer to the Target cell and its properties using the range name "Target"

    The first line of code checks the row of the Target cell. If it is 1, it exits the routine ... this avoids corrupting the header row.

    The next line of code then checks if the cell being changed is in column 21 (U). If it is, it switches off Events so that the code won't be actioned twice, clears the cell 4 columns away, and switches Event handling back on again.

    Hope this clarifies.

    Regards

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,213

    Re: automatically change selected values in column Y when values in Column U change

    Many people do abort the ws-change macros if more than one cell is changed at a time, I prefer not to do that. My version will analyze all the cells changed at once and consider them all individually.

    All my sub is doing is check to see if the "cell" changed was in column 21 (col U), and if so, empty column 25 (Y) of that same row.

    Private Sub WorkSheet_Change(ByVal Target as Range)
    Dim Cell As Range
    
    Application.EnableEvents = False
    For Each Cell In Target     'process each changed cell one at a time
      'if the cell is in column U, then empty column Y of the same row
        If cell.Column = 21 Then Cells(cell.Row, 25).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!)

+ 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.2.0