0

I have looked up the question and have seen several solutions addressing things like Select or having protected worksheets, none of which apply to me here.

For various reasons, I can't post the entire code, but I will give a description of what it does and post the exact sub that is giving me issues.

I have a Macro that generates a number of worksheets based on the Month and Year input by the user (so "1" - "31" or "1" - "30" etc). To generate these worksheets, the macro makes copies of a worksheet fittingly named "EXAMPLE". One thing that is copied is a picture (just a rectangle with the word 'Export' on it) that has a macro attached to it.

I recently made what I thought was a cosmetic change by moving the location of this picture, since then, when I run the macro I get an error:

"Run-time error '1004': Microsoft Excel cannot paste the data."

And options for 'End' 'Debug' and 'Help'

If I select 'Debug' it points me to a second macro which is called during the process of the generation macro'

Sub CopyAllShapes()
Dim ws As Worksheet

' Sets the non-generated worksheets as an array
nSheets = Array("EXAMPLE", "Weekly Totals", "Menu")

' Copies the Picture from the EXAMPLE sheet to all worksheets not in the array and then assigns a 
' seperate Macro called "Export" to the picture on each of these sheets.
For Each ws In ActiveWorkbook.Worksheets
    If Not IsNumeric(Application.Match(ws.Name, nSheets,0)) Then
        Sheets("EXAMPLE").Shapes("Picture 1").Copy
        ws.Range("J62").PasteSpecial
        ws.Shapes("Picture 1").OnAction = "Export"
    End If
Next ws

Application.CutCopyMode = xlCopy
End Sub

The Debug option highlights the line

ws.Range("J62").PasteSpecial

What really confuses me is that if I select 'End' instead of 'Debug', the macro stops, but all the the sheets have had the picture pasted as well as the Export Macro assigned and everything works as expected. If I were the only person using this, it would be a minor annoyance, but this document is used by many people that can't reliable be told to "just ignore" the error. Since the macro is functioning as expected, how can i troubleshoot what is causing the problem and make the error go away?

As I said, I can't post the entire macro, but I can post some bits and pieces if anyone needs more info.

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • I ran the code and added a shape called "Picture 1" and it pasted just fine for me. – Ethan Feb 04 '20 at 19:01
  • Does it work if you move it back? – PeterT Feb 04 '20 at 19:07
  • @PeterT Moving it back isnt really an option because of the way I had to adjust the cells. However, I still have a saved copy of the Workbook pre-changes and it works fine there. – Brandon Murphy Feb 04 '20 at 19:12
  • @Ethan the pasting isn't actually the problem...like I said, mine pastes as well, even though it says it does not. That is one reason I am so lost. – Brandon Murphy Feb 04 '20 at 19:12
  • I don't get any error. I am using Excel 2016. What version are you using? I remember having some errors with macros in Excel 2007 that normally run in 2016. By the way, I would change 'Dim ws As Worksheet' to 'Dim ws As Variant'. – Shelty Feb 04 '20 at 19:22
  • @PeterT I went ahead and tried pasting the picture using the same code as in the Workbook that functions properly. Literally the only thing that changed is the cell to be pasted into (R47 instead of J62) and I have the same issue. This leads me to think that maybe some of the changes I made were not so cosmetic after all. – Brandon Murphy Feb 04 '20 at 19:26
  • You could bracket this section of code with error handling to catch this error. If you get the error, then check the "new" copied worksheet to see if the shape really was copied. If it was copied, then suppress the error. – PeterT Feb 04 '20 at 19:27
  • @Shelty I am using Excel 2013; but unfortunately I don't think that is a factor since it works on one Workbook but not the other. As for changing from 'Dim ws As Worksheet' to 'Dim ws As Variant' what is the reason for this? I'm not questioning your advice, just trying to learn and understand – Brandon Murphy Feb 04 '20 at 19:29
  • 1
    @BrandonMurphy - `Dim ws as Worksheet` is the right thing to use. – BigBen Feb 04 '20 at 19:30
  • 1
    @BrandonMurphy As I said, some things that work in Excel 2016 do not in 2013. Sometimes just because of bugs, I guess. Even if not, changing seemingly unimportant things (for a newbie like me) may help. When having similar errors I normally change code a bit. Maybe try this [approach](https://stackoverflow.com/questions/37444009/vba-runtime-error-1004-microsoft-excel-cannot-paste-the-data). Instead of 'ws.Range("J62").PasteSpecial', I would add three lines 'Set myPic = ws.Pictures.Paste', 'myPic.Left = ws.Range("J62").Left', 'myPic.Top = ws.Range("J62").Top' and declare myPic As Picture. – Shelty Feb 04 '20 at 20:24
  • Do you have Skype running? I have found that sometimes Skype for Business reacts to copying data (in *any* program) by "peeking" at the clipboard, which locks it out and prevents me from being able to paste - and the error when pasting wipes the clipboard. Telling the macro to `Sleep` briefly and/or `DoEvents` allows it to "catch up" (sometimes). For other cases, I use error handling to retry the `Copy` – Chronocidal Feb 05 '20 at 13:55
  • @Chronocidal No Skype. It is disabled on the system (work computer). I suppose it is possible there is another program that might be doing something similar, but that does not explain why it works using the old workbook but not the new one. – Brandon Murphy Feb 05 '20 at 16:08

3 Answers3

1

On moving to Office 365 and Win10 (can't say which of those was the culprit) I found a bunch of existing macros which would give that same error when trying to paste a copied image onto a worksheet.

When entering debug, the "paste" line would be highlighted, but if I hit "Continue" it would (after one or two attempts) run with no errors.

I ended up doing this:

'paste problem fix
Sub PastePicRetry(rng As Range)
    Dim i As Long
    Do While i < 20
        On Error Resume Next
        rng.PasteSpecial
        If Err.Number <> 0 Then
            Debug.Print "Paste failed", i
            DoEvents
            i = i + 1
        Else
            Exit Do
        End If
        On Error GoTo 0
        i = i + 1
    Loop
End Sub

...which looks like overkill but was the only reliable fix for the problem.

EDIT: cleaned up and refactored into a standalone sub.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Where did you put this? Just after the code I posted above? Additionally, the number of times I would need this to loop woudl vary depending on the month (29 times for this month for example) so for the line you have "If i > 20 Then Exit Do" could I replace that with "If i >Days Then Exit Do" where I set Days as a variable that is pulled from a cell? – Brandon Murphy Feb 05 '20 at 13:27
  • @tim this will leave OERN active when it pastes without error? – chris neilsen Apr 20 '21 at 04:18
  • @chrisneilsen - pretty sure OERN doesn't travel "up" to the calling code but I will double-check – Tim Williams Apr 20 '21 at 06:05
  • @tim didn't mean to imply it "travels up" (it doesn't). If that's the entirety of the Sub, then no issue here. – chris neilsen Apr 20 '21 at 06:38
1

Not a pure fix, but this code will retry the Copy/Paste if it fails (up to 3 times), instead of just dropping it:

Const MaxRetries AS Long = 3

Sub CopyAllShapes()
    Dim ws As Worksheet
    Dim TimesRetried As Long

    ' Sets the non-generated worksheets as an array
    nSheets = Array("EXAMPLE", "Weekly Totals", "Menu")

    ' Copies the Picture from the EXAMPLE sheet to all worksheets not in the array and then assigns a 
    ' seperate Macro called "Export" to the picture on each of these sheets.
    For Each ws In ActiveWorkbook.Worksheets
        If Not IsNumeric(Application.Match(ws.Name, nSheets,0)) Then
            TimesRetried = 0
CopyExampleShape:
            On Error Resume Next
            Sheets("EXAMPLE").Shapes("Picture 1").Copy
            ws.Range("J62").PasteSpecial
            'If the Copy/Paste fails, retry
            If Err Then
                On Error GoTo -1 'Clear the Error
                'Don't get stuck in an infinite loop
                If TimesRetried < MaxRetries Then
                    'Retry the Copy/paste
                    TimesRetried = TimesRetried + 1
                    DoEvents
                    GoTo CopyExampleShape
                End If
            End If
            On Error GoTo 0
            ws.Shapes("Picture 1").OnAction = "Export"
        End If
    Next ws

    Application.CutCopyMode = xlCopy
End Sub

I have come across a similar issue before, and it was been down to another program (in one case Skype) reacting to data being added to the Clipboard by "inspecting" it. That then briefly locked the clipboard, so the Paste/PasteSpecial operation failed. This then caused the Clipboard to be wiped clean... All without Excel doing anything wrong.

"It is possible to commit no mistakes and still lose. That is not a weakness; that is life." ~ Jean-Luc Picard

Chronocidal
  • 6,827
  • 1
  • 12
  • 26
0

Just wanted to let everyone know I have found a (sort of) solution. Based on the answers/comments from Tim Williams and PeterT I modified the code to look like this:

Sub CopyAllShapes()
Dim ws As Worksheet

' Sets the non-generated worksheets as an array
nSheets = Array("EXAMPLE", "Weekly Totals", "Menu")

' Copies the Picture from the EXAMPLE sheet to all worksheets not in the array and then assigns a 
' seperate Macro called "Export" to the picture on each of these sheets.
For Each ws In ActiveWorkbook.Worksheets
    If Not IsNumeric(Application.Match(ws.Name, nSheets,0)) Then
        Sheets("EXAMPLE").Shapes("Picture 1").Copy
    On Error Resume Next
        ws.Range("J62").PasteSpecial
    On Error Goto 0
        ws.Shapes("Picture 1").OnAction = "Export"
    End If
Next ws

Application.CutCopyMode = xlCopy
End Sub

This has successfully ignored the error and everything is working properly now! Thanks everyone for your help, hopefully this aids someone else in the future!

  • If the paste fails doesn't the next `OnAction` line fail ? – Tim Williams Feb 05 '20 at 15:58
  • @TimWilliams it does not appear so. That's what has been so confusing about this, nothing is actually failing at all, I just keep getting an error saying it did. so perhaps if the paste WAS failing then the OnAction would fail as well, but as far as I can tell, it has never actually failed. – Brandon Murphy Feb 05 '20 at 16:02