Intersect Method with Worksheet_Change

  • Hi there once again.


    I hope that someone can spot my coding error. This same code worked fine last week and I'm not sure what has changed. As you can see by my probably ridiculous code below, I am trying to clear the contents of a range of cells based on the entry in another range of cells.


    It is now not only clearing the range of cells that I expect but another random 3 or 4 other ranges.


    As you can see, when completed I want to address 6 different cell ranges on the same worksheet and have only certain contents cleared and subsequent cells moved up. The workbook is too large to post but here is my code so far.


    Oh, and if you can suggest code improvements, I would be ever so grateful. I am not a programmer - just a lowly engineer trying to wear a programming hat!


    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    Is this all the code?


    Why are you only looking at IntersectRange1?

    Boo!:yikes:

  • Re: Intersect Method with Worksheet_Change


    MY guess is that perhaps more than one cell is being changed, and so more than one If condition gets invoked.


    Have you stepped through it?

    HTH


    Bob

  • Re: Intersect Method with Worksheet_Change


    I am only looking at IntersectRange1 to start with. As soon as I am able to debug that, then I will add the rest of the Intersect Ranges.


    And, yes, I understand that more than one cell is getting changed but in trying to debug, I have only changed a single cell but more than one is being triggered.


    Stepping thru is difficult when it's a workbook_change event. Or at least I haven't figured out how to step through an event.


    Thanks for all your help (and continued help?)

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    To step through just put a breakpoint (F9) somewhere.


    To check if only one cell has been changed.

    Code
    If Target.Count>1 Then Exit Sub


    By the way did you try the code I posted?

    Boo!:yikes:

  • Re: Intersect Method with Worksheet_Change


    Yes, I just did, Norie and I got a type mismatch at line:


    Code
    Range("A" * lRow & ":I15").PasteSpecial xlPasteValues


    Thanks,


    Sue

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    Gee, Norie, you think I could have spotted that for myself! Duh! Anyway, by adding the

    Code
    If Target.Count>1 Then Exit Sub


    the macro exits even though I am adding an "x" in only one of the intersect cells so it must be looping somewhere even with the

    Code
    Application.EnableEvents = False

    statement.


    Thanks so much for your help!

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    When I paste the code into the worksheet module it appears to work.


    It clears the row I enter a value in column J and then moves the other rows up.


    Is that what it's meant to do?

    Boo!:yikes:

  • Re: Intersect Method with Worksheet_Change


    Exactly! So I wonder what's up with that? Did you have the


    Code
    If Target.Count>1 Then Exit Sub

    included when you ran it? I guess that doesn't make any difference since I just commented that out and now it doesn't run at all.


    I just have no idea what is going on right now. I have the following:


    And nothing is happening. I cut and pasted your code and made no changes except for the typo. I'm confused (to say the least).


    Thanks,

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    You are probably firing off cascaded events when you change other cells, and you test the intersect wrongly.


    This should do what you want.


    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Range1 As Range
    Dim Range2 As Range
    Dim Range3 As Range
    Dim Range4 As Range
    Dim Range5 As Range
    Dim Range6 As Range

    On Error GoTo ws_exit
    Application.EnableEvents = False

    Set Range1 = Range("J3:J16")
    Set Range2 = Range("T3:T16")
    Set Range3 = Range("J20:J32")
    Set Range4 = Range("T20:T32")
    Set Range5 = Range("J36:J50")
    Set Range6 = Range("T36:T50")

    If Not Intersect(Range1, Target) Is Nothing Then
    Application.EnableEvents = False
    Range("A" & Target.Row).Resize(, 10).ClearContents
    Range("A" & Target.Row & ":I16").Copy
    Range("A" & Target.Row & ":I15").PasteSpecial xlPasteValues
    Range("A16:I16").ClearContents
    Range("A3").Select
    Application.EnableEvents = True
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]

    HTH


    Bob

  • Re: Intersect Method with Worksheet_Change


    I commented out all my code, cut and pasted yours in and nothing is happening. I don't get any errors but nothing happens on my worksheet either.


    Thanks so much for your help!

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    Blue


    Where is the code located?


    It should be in the worksheet module which it's supposed to work on.


    When I used it I did have the Count check.


    Perhaps you've not turned events back on at some point when debugging?

    Boo!:yikes:

  • Re: Intersect Method with Worksheet_Change


    Ok, figured out one thing that was wrong. Somewhere along the line

    Code
    application.enableevents = true

    did not happen. When I manually added that, at least something happened. The row I wanted cleared was cleared but the subsequent rows did not move up one row so now I have the worksheet with a blank row in the middle of the range.

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    Missed a 1


    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Range1 As Range
    Dim Range2 As Range
    Dim Range3 As Range
    Dim Range4 As Range
    Dim Range5 As Range
    Dim Range6 As Range

    On Error GoTo ws_exit
    Application.EnableEvents = False

    Set Range1 = Range("J3:J16")
    Set Range2 = Range("T3:T16")
    Set Range3 = Range("J20:J32")
    Set Range4 = Range("T20:T32")
    Set Range5 = Range("J36:J50")
    Set Range6 = Range("T36:T50")

    If Not Intersect(Range1, Target) Is Nothing Then
    Application.EnableEvents = False
    Range("A" & Target.Row).Resize(, 10).ClearContents
    Range("A" & Target.Row + 1 & ":I16").Copy
    Range("A" & Target.Row & ":I15").PasteSpecial xlPasteValues
    Range("A16:I16").ClearContents
    Range("A3").Select
    Application.EnableEvents = True
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]

    HTH


    Bob

  • Re: Intersect Method with Worksheet_Change


    That did it! Thanks so much to the both of you! Now I think I can carry this on following the logic and complete the assignment. Bless you all!

    Thanks,


    Blue

  • Re: Intersect Method with Worksheet_Change


    Ok, all is working with the exception of range 6 which hangs up just before the paste special and I have checked it and checked it again and again and can't find anything different from the preceeding code. Here it is in it's new entirety thanks to you guys. Now just the final error and I will leave you all alone for a while with great appreciation!

    Thanks,


    Blue

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!