在 excel 个工作簿之间复制和粘贴

Copy and pasting between excel workbooks

我有一个宏可以从一个 excel 文档复制并粘贴到另一个文档。出于某种原因,在从其他源文档复制和粘贴后立即使用 pastespecial 时出现错误。因此,作为解决方法,我只是正常粘贴,然后再次复制它,然后使用 pastespecial。我的问题是,当 运行 这个宏出于某种原因时,它会在数字的末尾添加一个 space ,将它们变成文本。这意味着我的图表无法识别它们。

Workbooks.Open (fileLocation & "/" & fileName & fileType)
Worksheets(sourceWorksheet).Select
rowInUse = 46           'Add data row and name of sheet being imported into
mySheet = "sheet2"
pasteLocation = "D5"
lastColumn = ActiveSheet.Cells(rowInUse,     Columns.Count).End(xlToLeft).Column
Range(Cells(rowInUse, firstColumn), Cells(rowInUse, lastColumn)).Copy
ActiveWorkbook.Close SaveChanges:=False
Worksheets(mySheet).Select
Range(tempPasteLocation).Select
ActiveSheet.Paste
    Sheets(mySheet).Select
    Range(tempPasteLocation, Cells(tempRow, tempColumn + lastColumn)).Select
    Selection.Copy
    Range(pasteLocation).Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
Range(tempPasteLocation, Cells(tempRow, tempColumn + lastColumn)).Select
Selection.ClearContents

有人知道为什么会发生这种情况或如何解决吗?

谢谢

执行直接值传输,而不是 Copy、PasteSpecial、Values。

替换,

Sheets(mySheet).Select
Range(tempPasteLocation, Cells(tempRow, tempColumn + lastColumn)).Select
Selection.Copy
Range(pasteLocation).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True

有了这个,

with workSheets(mySheet)
    with .Range(tempPasteLocation, .Cells(tempRow, tempColumn + lastColumn))
        .Range(pasteLocation).resize(.rows.count, .columns.count) = .value2
    end with
end with