+ Reply to Thread
Results 1 to 6 of 6

Copy Paste between workbooks fails intermittently

  1. #1
    Registered User
    Join Date
    02-15-2019
    Location
    Quebec, Canada
    MS-Off Ver
    2010 Pro
    Posts
    12

    Copy Paste between workbooks fails intermittently

    I am trying to copy from an external file and paste into the currently open file.

    The code I'm using is below. Sometimes it works and sometimes it doesn't.

    I'm still quite new to VBA and really not sure what the issue was... but I was suspecting the multiple connections to the same file. I first connect when I'm asking the user to select what sheet to use. I then connect again for each column to copy and paste. I tried setting up global variables for the worksheet and workbook variables so that I only had to SET them once. That seemed to work for a while and now the error is back.

    You'll see that I am not doing a direct copy - paste (in one line). This always failed. I came up with a solution that was to Copy first, then select the active sheet and then the cell and finally just do an activesheet.paste. This was working but is now where the program fails intermittently with the error "Copy method of range class failed".

    Other observations:

    1. I can't get rid of the clipboard warning when the script ends despite using "Application.DisplayAlerts = False" as well as "Application.CutCopyMode = False" in several placees.
    2. Sometimes when I go to the debug window, I can just click the "play" button and then the code finishes properly. Other times, it stays stuck on that activesheet.paste line.
    3. When the code fails, I think there is a hanging open connection to the external WS. I figure this by seeing that when I close and open Excel, I get that external WS opening before the file I've selected. In this state, issue seems to happen more often.

    Let me know if I can provide any other helpful information.

    Thanks!



    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    02-15-2019
    Location
    Quebec, Canada
    MS-Off Ver
    2010 Pro
    Posts
    12

    Re: Copy Paste between workbooks fails intermittently

    OK figured out why I was still getting the warning about the clipboard...

    need this: oApp.CutCopyMode = False

    Instead of this: Application.CutCopyMode = False

    I realized I was setting the cutcopymode to false on the current application and not the one I had opened... duh!

  3. #3
    Registered User
    Join Date
    02-15-2019
    Location
    Quebec, Canada
    MS-Off Ver
    2010 Pro
    Posts
    12

    Re: Copy Paste between workbooks fails intermittently

    Also, I realized that the loop will often work a couple times, pasting into a couple columns before failing. Often pressing the "play" button will get it to finish off the rest of the column ok.

  4. #4
    Registered User
    Join Date
    02-15-2019
    Location
    Quebec, Canada
    MS-Off Ver
    2010 Pro
    Posts
    12

    Re: Copy Paste between workbooks fails intermittently

    No ideas?

    I've tried so many variations of range and value and special pasting.. using variables or not, one line copy/paste, using "=" instead of copy paste etc.. etc...

    Here's something that I finally tried in desperation that seems to actually be working so far:

    Added a timer just before the paste.

    Giving it a 1 sec delay before pasting seems to have resolved my issue!

    Maybe that helps identify the real problem???

    Here's the code I'm using to wait:

    Application.Wait (Now + TimeValue("0:00:01"))

  5. #5
    Registered User
    Join Date
    12-04-2020
    Location
    Mannheim, Germany
    MS-Off Ver
    2016
    Posts
    1

    Re: Copy Paste between workbooks fails intermittently

    Thanks for this!
    I am still trying to figure out how to add the pause only when an error occurs, but for now this is doing the trick!
    Cheers

    For all future people looking for a good answer, this is how I have finally solved the issue (which only occurs intermittently - perhaps 1 in 50 cycles)

    This error only occurs when Im running a macro which opens other workbooks that are not visible and copying columns of data to a temp sheet in my main workbook... I process this afterwards and then open the next workbook.



    Set Rng1 = Sh_2.Range(Sh_2.Cells(2, AUCol), Sh_2.Cells(LR, SerCol)).SpecialCells(xlCellTypeVisible) '(Rng1 is in Wkbk2)

    Rng1.Copy
    On Error GoTo CopyPasteError1
    Waypoint1:
    With WKBK1
    .Sheets("Temp").Cells(1, 1).PasteSpecial Paste:=xlPasteValues
    End With
    C1 = 0

    'rest of the code

    Exit sub

    CopyPasteError1:
    C1 = C1 + 1
    If C1 = 100 Then 'C1 is here incase the issue is severe and will otherwise repeat until the computer melts. This gives a chance to inspect the problem when the counter reaches 100.
    DoEvents
    Stop
    End If
    Application.Wait (Now + TimeValue("0:00:01"))
    Resume Waypoint1

    End Sub

    To summarize my problem, occasionally but too often to ignore, the program would have an error when trying to paste data from Wkbk1 to Wkbk2.
    Muse43 (above) gave the great tip which looks good, but as I am doing a lot of copying and pasting from many many sheets and the job takes several hours as is, I didn't want to add 2 seconds (I found 1 second often wasn't enough) to every copy / paste. Now with this, it does it only when required and as often as necessary (up to 100 cycles). This is of course changeable to whatever you think is reasonable.

    Thanks again Muse43 - Saved me!.
    Last edited by MarkBrown71; 12-04-2020 at 11:48 AM.

  6. #6
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Copy Paste between workbooks fails intermittently

    I have found that using 'activesheet.paste' will cause problems sometimes. I was always able to cure the problem by using 'activesheet.PasteSpecial' instead

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copy/Paste macro fails in one app, but not another
    By bgoldsmi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2018, 10:31 AM
  2. [SOLVED] ws.Paste method fails after lots of copy-paste of shapes
    By bagullo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-15-2015, 03:00 AM
  3. [SOLVED] Open Multiple Workbooks, Record names, Copy paste to Active Workbook, Close the Workbooks
    By vba_madness in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2013, 06:09 AM
  4. setting chart series formula fails intermittently in Excel 2007/2010
    By xlrotor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-19-2012, 03:51 PM
  5. Copy paste code fails
    By Ron Dean in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-17-2006, 07:50 AM
  6. Filter Copy/Paste Fails - Two Ranges Selected?
    By Craigm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-15-2005, 11:16 AM
  7. Replies: 6
    Last Post: 04-05-2005, 03:06 PM

Tags for this Thread

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