on change event, funny behaviour

JRRyan

Board Regular
Joined
Jul 12, 2010
Messages
55
Hi,

I have a bit of code that triggers as part of a worksheet_change function. Here it is:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
 
If Not Intersect(Target, Range("H_23")) Is Nothing Then
If ((IsEmpty(Target.Value) = False) And (IsNull(Target.Value) = False)) Then
Application.Run "AddQ1DescriptionMacro"
End If
End If
 
End Sub

Now, what is supposed to happen is - if I add some text to the cell named "H_23" then it will trigger the AddQ1DescriptionMacro and the cells contents will be pasted to a list on another worksheet. Where as if I delete existing contents from the same cell, nothing will trigger and no other change will be made beyond deleting the contents of the cell.

This works perfectly... except...

I have another bit of code designed to clear certain contents from the entire sheet and reset it back to default ready for a new bit of data. It looks something like this:

Code:
 Sub Clearing()
   Range("H_4_2").Select
    Selection.ClearContents
 
    Application.ScreenUpdating = False
 
    Range("RNG_1").Select
    Selection.ClearContents
    Range("RNG_2").Select
    Selection.ClearContents
    Range("H_17").Select
    Selection.ClearContents
    Range("H_19").Select
    Selection.ClearContents
    Range("F_1").Select
    Selection.ClearContents
    Range("F_2").Select
    Selection.ClearContents
    Range("RNG_3").Select
    Selection.ClearContents
    Application.ScreenUpdating = True
    ActiveWorkbook.Save
End Sub

The issue is this. If I manually delete one cell at a time, the code works fine. If, however, I delete a range of cells (as is the case with my named range "RNG_3" in the Clearing code) then the worksheet change triggers and adds/copies a blank line to my other sheet from every cell in the range.

I'm guessing it has to do with how intersect works with a range of cells as opposed to individual ones, but I'm kind of baffled as to how, or what I would do to change it.

My solution now is to list each cell in the range individually instead of referencing them as a range. Can anyone offer a better solution?
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
[COLOR="Red"][B]Application.EnableEvents = False[/B][/COLOR]
'your code here
[COLOR="Red"][B]'Application.EnableEvents = True[/B][/COLOR]
End Sub

Also your second piece of code could be tidied up and made more efficient:

Code:
 Sub Clearing()
   Range("H_4_2").Select
    Selection.ClearContents
 
    Application.ScreenUpdating = False
 
    Range("RNG_1").ClearContents
    Range("RNG_2").ClearContents
    Range("H_17").ClearContents
    Range("H_19").ClearContents
    Range("F_1").ClearContents
    Range("F_2").ClearContents
    Range("RNG_3").ClearContents
    Application.ScreenUpdating = True
    ActiveWorkbook.Save
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top