Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Jun 2006
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Skip Change Event (2003)

    I have a function that does Range("A2:A71").ClearContents. However, I also have a worksheet_Change event that does UpperCase when a cell in column A is changed. I added code to the change event so that it would not do the UpperCase if the cell was cleared (If Not Intersect(Target, Range("A2:A71")) = "" Then). This worked when clearing a cell one by one, but when I run the Range("A2:A71").ClearContents I am getting an error because of the if statement I mentioned. Is there a way to either change the code in the change event to work or is there a way to skip the worksheet change event when I run the Range("A2:A71").ClearContents? Thanks!!

    Chris

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Skip Change Event (2003)

    This:
    <code>If Not Intersect(Target, Range("A2:A71")) = "" Then</code>
    should be:
    <code>If Not Intersect(Target, Range("A2:A71")) Is Nothing Then</code>

    but the simplest way is to use:
    <pre>Application.EnableEvents = False
    Range("A2:A71").ClearContents
    Application.EnableEvents = True
    </pre>

    HTH
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Skip Change Event (2003)

    You'll have to loop through the cells of the intersection:

    Dim oCell As Range
    If Not Intersect(Target, Range("A2:A71")) Is Nothing Then
    For Each oCell In Intersect(Target, Range("A2:A71"))
    If Not oCell = "" Then
    ...
    End If
    Next oCell
    End If

    Added: Rory's suggestion is much better

  4. #4
    Star Lounger
    Join Date
    Jun 2006
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Skip Change Event (2003)

    Thanks Rory! The enable events is exactly what I needed. Not only does it work correctly now, it also significantly sped up the function!!

  5. #5
    Star Lounger
    Join Date
    Jun 2006
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Skip Change Event (2003)

    Thanks Hans! Good to know for future reference and I agree that Rory's is more efficient. Thanks!

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Skip Change Event (2003)

    You should do both of Rory's suggestions AND Hans' suggestion.
    Legare Coleman

Posting Permissions

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