+ Reply to Thread
Results 1 to 5 of 5

Copy/Paste Special on Worksheet Deactivation

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    Copy/Paste Special on Worksheet Deactivation

    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.
    Last edited by TheRobsterUK; 02-06-2011 at 08:10 AM.

  2. #2
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    Re: Copy/PasteSspecial on Worksheet Deactivation

    Simply using a Range=Range approach works without any problems though, e.g. on the active worksheet I am navigating away from, this embedded code works fine:

    Range("J9") = Range("I9")
    I.e. it makes cell J9 equal to cell I9. Wonder if the original problem has something to do with the copy/paste functions then?

    *edit*

    This is also works but copies the cells verbatim (including formatting) which isn't what I want, I just want to copy/paste the values:

    Range("source").Copy Range("target")
    Seems to be some problem with the pastespecial function then and it makes it just repeat the command over and over many times....?

    Thanks again
    Rob

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy/PasteSspecial on Worksheet Deactivation

    pastespecial method can not force the code to restart in deactivation event..you'd better posted your workbook with abnormal code functioning

  4. #4
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    Re: Copy/PasteSspecial on Worksheet Deactivation

    Right, got it sorted. Can't believe how simple it was in the end.

    Private Sub Worksheet_Deactivate()
        Range("target").Value = Range("source").Value
    End Sub

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: *resolved* Copy/PasteSspecial on Worksheet Deactivation

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1