Consulting

Results 1 to 9 of 9

Thread: clear contents of adjacent cell(s) on change

  1. #1

    clear contents of adjacent cell(s) on change

    Hi,

    I have a drop down list in column G, a dependent list in column H (using INDIRECT) and
    another dependent list in column I (using SUBSTITUTE). 1000 Rows.

    I'd like to clear columns H & I when column G is changed and also clear column I when only
    column H is changed.

    Any help appreciated

    thanks
    Jon

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Private Sub Worksheet_Change(ByVal Target As Range)
      Application.Enable Events = False
        If Not Intersect(Target, Range("G:G")) Is Nothing Then
          Range("H:I").ClearContents
          GoTo AllDone
        End If
    
        If Not Intersect(Target, Range("H:H")) Is Nothing Then Range("I:I").ClearContents
    
    AllDone:
      Application.EnableEvents = True
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Apologies - might not have been specific enough.

    The code bugs out if I leave the 'Application.EnableEvents' in andif I take out although it does work it clears everything i.e. the whole column(s)

    What I wanted was something similar but for separate rows e.g. change G2 - H2 & I2 clear and not the whole columns, and if I change G14, only H2 & I2 would be affected.

    thanks
    Jon
    Last edited by blackie42; 12-08-2014 at 03:19 AM.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The code bugs out
    I don't understand.


    it does work it clears everything i.e. the whole column(s)
    As planned.



    change G2 - H2 & I2 clear and not the whole columns
    Private Sub Worksheet_Change(ByVal Target As Range)
      Application.Enable Events = False
        If Not Intersect(Target, Range("G:G")) Is Nothing Then _
          Range("H2:I2").ClearContents
    
      Application.EnableEvents = True
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Perhaps you mean:
    Private Sub Worksheet_Change(ByVal Target As Range)  
    Application.Enable Events = False
    If Not Intersect(Target, Range("G:G")) Is Nothing Then
    Target.Offset(, 1).Resize(, 2).ClearContents
    GoTo AllDone
    End If
    If Not Intersect(Target, Range("H:H")) Is Nothing Then Target.Offset(, 1).Resize(, 1).ClearContents
    AllDone:
    Application.EnableEvents = True
    End Sub
    Be as you wish to seem

  6. #6
    Thanks Guys - probably didn't explain too well.

    Aflatoons extended code using resize does the job

    regards
    Jon

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Yeah, you really have to work on explaining.
    clear columns H & I when column G is changed and also clear column I
    change G2 - H2 & I2 clear and not the whole columns, and if I change G14, only H2 & I2 would be affected.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Are you missing an End If for the second If not intersect?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    @ Ted,

    No, it's a one line If.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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