将粘贴复制为值Excel VBA

时间:2017-05-11 13:49:37

标签: vba excel-vba excel

我有这段代码,但我需要它将数据粘贴为值。我知道它可能很简单,但我没有使用.Paste函数,所以我不知道如何转换它。我处于初级阶段。非常感谢提前!

Sub Movetabletototal()
    Dim Count As Integer
    Dim copyRng As Range, pasteRng As Range
    Dim totalWS As Worksheet, mixerWS As Worksheet

    Set totalWS = Worksheets("TOTAL")
    Set mixerWS = Worksheets("MIXER TOTAL")

    Set copyRng = mixerWS.Range("P3:Q" & mixerWS.Cells(mixerWS.Rows.Count, 17).End(xlUp).Row)

    Dim newRow As Long
    newRow = totalWS.Cells(totalWS.Rows.Count, 1).End(xlUp).Row
    If newRow > 1 Then newRow = newRow + 1

    copyRng.Copy totalWS.Range(totalWS.Cells(newRow, 1), totalWS.Cells(newRow + copyRng.Rows.Count, copyRng.Columns.Count))
End Sub

2 个答案:

答案 0 :(得分:1)

您需要在代码中使用 PasteSpecial 。将最后一行更改为:

'.
'. Your code will be the same till here
'.

copyRng.Copy

Set pasteRng = totalWS.Range(totalWS.Cells(newRow, 1), _
totalWS.Cells(newRow + copyRng.Rows.Count, copyRng.Columns.Count))

pasteRng.PasteSpecial xlPasteValues

End Sub

或者您可以使用以下值(设置上述范围):

pasteRng.Value = copyRng.Value

答案 1 :(得分:1)

如果您只对感兴趣,则没有理由使用CopyPastePasteSpecial。相反,只需使用直接值赋值:

Dim destinationRange as Range
With totalWs
    Set destinationRange = .Cells(newRow, 1).Resize(copyRange.Rows.Count, copyRange.Columns.Count)
End With

destinationRange.Value = copyRange.Value