1004 Pastespecial of range failed

时间:2017-04-18 02:03:59

标签: excel excel-vba vba

几天来,复制和粘贴时会发生此错误。我搜索了互联网并尝试了不同的方法,但仍然无法弄清楚。

我刚刚在声明之后添加on error resume next作为临时修复程序,程序似乎运行正常,但究竟是什么导致了这个?

iRow = wsDest.Range("A65536").End(xlUp).Row + 1
lColumn = wsSource.Cells(1, Columns.Count).End(xlToLeft).Column

For sRow = 2 To wsSource.Range("A65536").End(xlUp).Row
    For iCol = 1 To lColumn
      wsDest.Cells(iRow, iCol) = wsSource.Cells(sRow, iCol)
      wsSource.Cells(sRow, iCol).Copy

      wsDest.Cells(iRow, iCol).PasteSpecial xlPasteFormats
     ' On Error Resume Next

    Next iCol
    wsDest.Cells(iRow, lColumn + 1) = Left(sFile, 6)

    iRow = iRow + 1

Next sRow

1 个答案:

答案 0 :(得分:0)

我猜你只需要这个......

lColumn = wsSource.Cells(1, Columns.Count).End(xlToLeft).Column
For sRow = 2 To wsSource.Range("A65536").End(xlUp).Row
    wsSource.Range(wsSource.Cells(sRow, 1), wsSource.Cells(sRow, lColumn)).Copy
    wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormats
    wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Next sRow

或者就是这样。无需遍历行和列。

lRow = wsSource.Range("A65536").End(xlUp).Row
lColumn = wsSource.Cells(1, Columns.Count).End(xlToLeft).Column

wsSource.Range("A2", wsSource.Cells(lRow, lColumn)).Copy
wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormats
wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False