使用粘贴特殊值

时间:2017-04-11 17:02:37

标签: excel vba excel-vba

感谢另一位用户,我已经能够使用目的地粘贴到偏移范围,现在我正在尝试更改它,以便它粘贴为值,以便我的格式和公式不会随身携带。由于公式复制,粘贴当前导致#REF错误。

Option Explicit
Sub CopyPasteOffset()
    Dim OffsetRange As Long
    OffsetRange = Cells(78,1).Value
    Range("B65:F65").Copy _
        Destination:=Sheets("stats FY2017").Cells(2+OffsetRange,2).PasteSpecial xlPasteSpecial
End Sub

给我一​​个"语句错误"

2 个答案:

答案 0 :(得分:1)

试试这个......

Sub CopyPasteOffset()
    Dim OffsetRange As Long
    OffsetRange = Cells(78, 1).Value
    Range("B65:F65").Copy
    Sheets("stats FY2017").Cells(2 + OffsetRange, 2).PasteSpecial xlPasteValues
End Sub

答案 1 :(得分:0)

以下是PasteSpecial xlValues的一些变体。有些包括格式化,但都采用原始中任何公式的值。

Sub CopyPasteOffset()
    Dim offsetRange As Long

    With Worksheets(1)
        offsetRange = .Cells(1, 1).Value

        'raw value transfer - slightly faster but dates become integers, time becomes doubles
        'and currency loses regional information
        With .Range("B1:F1")
            Worksheets(2).Cells(1 + offsetRange, 1).Resize(.Rows.Count, .Columns.Count) = .Value2
        End With

        'value transfer - dates, times and currency are retained
        With .Range("B1:F1")
            Worksheets(2).Cells(1 + offsetRange, 1).Resize(.Rows.Count, .Columns.Count) = .Value
        End With

        'add a blank cell and paste the union - provides Value and Formatting transfer (strips formulas)
        '(assumes that Z1 is a blank cell that will not interfere with anything when pasted)
        .Range("B1:F1, Z1").Copy _
            Destination:=Worksheets(2).Cells(1 + offsetRange, 1)

        'PasteSpecial Values, optionally paste formats
        .Range("B1:F1").Copy
            With Worksheets(2).Cells(1 + offsetRange, 1)
                .PasteSpecial xlValues
                .PasteSpecial xlFormats
            End With
    End With

End Sub