简体   繁体   中英

Copy paste as values Excel VBA

I have this code but I need it to copy paste the data as values. I know it probably is very simple, but I have not used .Paste function, so I do not know how to convert it. I am at a very beginner level. Many thanks in advance!

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

You need to use PasteSpecial in your code. Change the last line to this:

'.
'. 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

Or you can use values like below (set the ranges like above):

pasteRng.Value = copyRng.Value

If you're only interested in the values there's no reason to use either Copy , Paste or PasteSpecial . Instead, just use a direct value assignment:

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

destinationRange.Value = copyRange.Value

You could also try

.PASTESPECIAL (-4163)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM