I have a worksheet that requries the user to input some data, then they need to select a different worksheet in order to carry out some calculations. I need a macro that copies/pastes values from the user input range to a different section of the user-input worksheet whenever the user selects a different worksheet.
Here's some code that I thought would work (code is located in the worksheet module):
Private Sub Worksheet_Deactivate()
'This macro runs on worksheet deactivation (i.e. when the user selects another
'worksheet either via a macro or clicking on a different worksheet tab
Dim HasRun As Boolean 'prevent macro from constantly re-running
If HasRun = True Then Exit Sub
'copy/paste ranges
Range("source").Copy
Range("target").PasteSpecial Paste:=xlPasteValues
HasRun = True
End Sub
It almost works but the macro repeats itself a large number of times, and also sometimes forces Excel to terminate. I can't understand why it does this though. Can anyone provide some advice? Essentially what I want is some code that copies/pastes a set of values from one range to another in the currently active worksheet before another worksheet is activated.
I put the HasRun variable in to try and get the routine to run only once but it doesn't really seem to have helped (still repeats many times...)
Thanks
Rob
Ps - I am using Excel 2007.
Bookmarks