CMarneras
CMarneras

Reputation: 3

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

Upvotes: 0

Views: 721

Answers (3)

Chris
Chris

Reputation: 254

You could also try

.PASTESPECIAL (-4163)

Upvotes: 0

M--
M--

Reputation: 28826

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

Upvotes: 1

David Zemens
David Zemens

Reputation: 53623

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

Upvotes: 2

Related Questions