Greetings,
When running the code below in a new workbook, with Sheet1 activated and with application.enableevents=True, the PasteSpecial line causes the Sheet1 and Sheet2 Worksheet_Activate codes to run (as well as their respective Worksheet_Deactivate events. Moreover, when the pastespecial line is executed, I can see the screen flash over to Sheet2 and then back go Sheet1, and following the flash is the execution of the 4 events (in the expected order: Sheet1 deactivate, Sheet2 activate, Sheet2 deactivate, Sheet1 activate). I put monitors in each event to print out which worksheet is active. All 4 events report that Sheet1 is the current activesheet. It's certainly odd to have the Sheet2 activate vent tell me that Sheet1 is the activesheet.
Obviously just turning events off would keep this from happening. I have actually done so in my much larger workbook (this is just a simple example to show the concept). However, I sometime get weird errors in the more complicated workbook where the code will suddenly error-out and the workbook will unexpectedly be on Sheet2. I've been able to isolate that the only thing that could be activating Sheet 2 is the pastespecial code demonstrated here.
Note: running the code below with Sheet2 as the activesheet does not trigger any of the vents to run, or for the screen to flash in anyway -- in otherwords, the copy line is clearly working just fine without some how having to go to Sheet1 to do its job.
FYI: Cell B2 in both worksheets simply has a value of 2. I can see mathematically the pastespecial/xlMultiply is doing its job as expected.
I'm using Windows 10 and Microsoft Office Professional Plus 2016
Any thoughts? Thank you for your time!
When running the code below in a new workbook, with Sheet1 activated and with application.enableevents=True, the PasteSpecial line causes the Sheet1 and Sheet2 Worksheet_Activate codes to run (as well as their respective Worksheet_Deactivate events. Moreover, when the pastespecial line is executed, I can see the screen flash over to Sheet2 and then back go Sheet1, and following the flash is the execution of the 4 events (in the expected order: Sheet1 deactivate, Sheet2 activate, Sheet2 deactivate, Sheet1 activate). I put monitors in each event to print out which worksheet is active. All 4 events report that Sheet1 is the current activesheet. It's certainly odd to have the Sheet2 activate vent tell me that Sheet1 is the activesheet.
Obviously just turning events off would keep this from happening. I have actually done so in my much larger workbook (this is just a simple example to show the concept). However, I sometime get weird errors in the more complicated workbook where the code will suddenly error-out and the workbook will unexpectedly be on Sheet2. I've been able to isolate that the only thing that could be activating Sheet 2 is the pastespecial code demonstrated here.
Note: running the code below with Sheet2 as the activesheet does not trigger any of the vents to run, or for the screen to flash in anyway -- in otherwords, the copy line is clearly working just fine without some how having to go to Sheet1 to do its job.
FYI: Cell B2 in both worksheets simply has a value of 2. I can see mathematically the pastespecial/xlMultiply is doing its job as expected.
I'm using Windows 10 and Microsoft Office Professional Plus 2016
Any thoughts? Thank you for your time!
Code:
Sub demonstration()
Sheet1.Range("B1").Copy
Sheet2.Range("B1").PasteSpecial Operation:=xlMultiply
End Sub