0
votes

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.

3
I ran the code and added a shape called "Picture 1" and it pasted just fine for me.Ethan
Does it work if you move it back?PeterT
@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
@BrandonMurphy - Dim ws as Worksheet is the right thing to use.BigBen
@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. 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

3 Answers

0
votes

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!

0
votes

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

0
votes

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.