复制并粘贴 Excel VBA 中的单元格值:编译和 运行 时间错误

Copy and Paste Cell Values in Excel VBA: Compile and Run Time Errors

我正在 excel VBA 中设置一个循环代码,该代码将从一个工作表中复制公式值并将这些值粘贴到另一个工作表上的 运行 列表中。我可以将公式复制并粘贴到所需位置,但我需要在此代码中更改什么以仅复制和粘贴值?

我尝试了此代码的多种变体:

  1. .Value复制后and/or在底部粘贴命令

  2. .PasteSpecial xlPasteValues(给出编译错误,预期语句结束)

  3. .Value 定义要复制的单元格时(给出 运行-时间错误“438”)

  4. 将我的复制和粘贴变量的 Dim 类型更改为 Variants、Strings 和 Range


Sub snapShot()
    Dim sourceSheet As String, destinationSheet As String
    Dim copyR As Range, copyT As Range
    Dim pasteR As Range, pasteT As Range
    sourceSheet = "REPORT"
    destinationSheet = "Historical Changes"

    Set copyR = Worksheets(sourceSheet).Range("I2")
    Set copyT = Worksheets(sourceSheet).Range("I3")

    With Sheets(destinationSheet)
        Set pasteR = .Cells(.Rows.Count, 1).End(xlUp)(2, 1)
        Set pasteT = .Cells(.Rows.Count, 1).End(xlUp)(2, 2)
    End With

    copyR.Copy pasteR.PasteSpecial xlPasteValues 'Compile Error: Expected: End of Statement
    copyT.Copy pasteT.PasteSpecial xlPasteValues 'Compile Error: Expected: End of Statement
    pasteT.Columns(2) = Now() 'This gives a time stamp next the my columns
    Application.OnTime Now() + TimeValue("24:00:00"), "snapShot"

End Sub

如果删除 .PasteSpecial xlPasteValues,我的代码将可以正常复制和粘贴公式,但由于我需要时间戳数据,所以我只需要值,而不需要公式。 我目前得到

Compile Error: Expected: End of Statement

如果我将其更改为 .Values,我会得到

Run-time error '438': Object doesn't support this property or method

如果您看到任何可以解决此问题的方法,请告诉我!我确定这很简单,但我已经在这个问题上停留了一段时间。

当使用 PasteSpecial 时,该位需要与 Copy 分开一行,如:

copyR.Copy
pasteR.PasteSpecial xlPasteValues

在这种特殊情况下,可以完全避免剪贴板,直接传输值,代码更短,操作更短。如:

pasteR.value=copyR.value