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

前端 未结 3 645
广开言路
广开言路 2021-01-27 13:30

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

3条回答
  •  清歌不尽
    2021-01-27 14:12

    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

提交回复
热议问题