Why is same ChangeEvent behaving differently under a different scenario

julhs

Active Member
Joined
Dec 3, 2018
Messages
454
Office Version
  1. 2010
Platform
  1. Windows
At moment I can’t get to bottom of it.
WHY;
if I manually insert blank cells into a “NamedRange” that my ChangeEvent calls all the associated call events, JUST the ONCE & Sub will End at command.
But if I manually Copy/Paste/Insert the EXACT same number of cells into EXACT same cells of the “NamedRange”, the same ChangeEvent is being fired TWICE before it will End Sub.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Post your code? Also, what do you mean by "manually insert" and "manually Copy/Paste/Insert"?
 
Upvote 0
Please show the code for the entire sub Worksheet_Change.

When you say "insert", are you actually inserting rows or columns in the middle of existing data, rather than overwriting existing cells? Can you give a concrete description of what cells are in "NamedRange", what it looks like to start with, and what it looks like after you update it?
 
Upvote 0
Thank you both for response.
Will take me a while to clean up/condense my code to something you can make use of.
But by “Manually” I mean, LeftClick select some cells and then select new location in the NamedRange, then RightClick and either select “Insert” or “Insert Copied cells”
Until I have clean/condensed my code for you to see, it’s not worth me saying any more.
 
Upvote 0
Not sure if this is enough for you to dissect the issue?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Some un-associated ChangeEvent code here.
If Not Intersect(Target, Range("ChemicalSuppliersList_Condensed")) Is Nothing Then
LastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Call DeletCellsAndFillDown
Call ReFormatRange
End If
' More un-associated Change code here
End Sub
If I only "Insert" blank cells into ("ChemicalSuppliersList_Condensed") then the ChangeEvent will end immediately on End Sub
But if I use "Copy/Paste/Insert" into ("ChemicalSuppliersList_Condensed") then the ChangeEvent will NOT end immediately on End Sub,
instead it goes back to "If Not Intersect(Target, Range("ChemicalSuppliersList_Condensed")) Is Nothing Then"
and the 2 call events are called again and only then does the Sub End
 
Upvote 0
Seems that I can replicate the behavior as well, so it's probably not your code. Not sure what is causing it.
 
Upvote 0
In some ways what you said gives me a degree of encouragement that it’s not simply of my code.
I’m currently experimenting with ways out of this issue, but would still value any further thoughts you may have.
 
Upvote 0
Did some more testing, it seems to have to do with what Right click > Insert copied cells does. If you record a macro and insert copied cells, you get something like this.
I can only come to the conclusion that Range.Insert does two changes to the sheet in sequence, probably first moving existing values (first Worksheet_Change triggers), and then pasting the new values (Worksheet_Change triggers again).
VBA Code:
Sub record_insertCopiedCells()
    Range("K7:R20").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("A5").Select
    Selection.Insert Shift:=xlDown
End Sub
 
Upvote 0
Again, what you said gives me a degree of encouragement.
That was sort of my thinking, HOWEVER I couldn’t come up with way counter act it.
 
Upvote 0
Not sure if this is enough for you to dissect the issue?
Many issues in Worksheet_Change are caused by unintended recursive calls when that Sub makes its own changes.

I did not dig down into the details but before you do anything else I would suggest that you add this at the beginning of the Sub
VBA Code:
Application.EnableEvents = False
and add this at the end
VBA Code:
Application.EnableEvents = True
If you still have the problem then I'll take your code and see if I can reproduce your problem.
 
Upvote 0

Forum statistics

Threads
1,221,568
Messages
6,160,551
Members
451,656
Latest member
SBulinski1975

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