Excel VBA: Copy and Paste a Range

One of the most common action you’ll need to learn is copying and pasting a range of data. It’s very easy to do this manually. In normal data, we use either CTRL + C to copy a selection of data and then use CTRL + V to paste the selected data in the target cell. It’s just as easy to copy and paste via VBA.

Range.Copy method

Range.Copy Method is a very convenient Method to copy and paste Range to destination in one line of code. All the formatting and formulas will be copied and pasted.

Syntax

expression.Copy (Destination)

Destination is optional. If this argument is omitted, Microsoft Excel copies the range to the Clipboard. It returns Variant.

Example 1: Copy Range and paste to another worksheet

The below code copy Sheet1 A1 to Sheet2 B1.

Sheets("Sheet1").Range("A1").Copy (Sheets("Sheet2").Range("B1"))

If there are more than one Range to copy, you just need to specific the first Range of the destination.

Sheets("Sheet1").Range("A1:B1").Copy (Sheets("Sheet2").Range("B1"))

Example 2: copy row

The below code copy from Sheet1 row 1:2 to Sheet2 row 6.

Note that if you copy a row to destination, the destination Range must be a Row or a Range in column A.

Sheets("Sheet1").Rows("1:2").Copy (Sheets("Sheet2").Range("A6"))

OR

Sheets("Sheet1").Rows("1:2").Copy (Sheets("Sheet2").Rows("6:6"))

OR

Sheets("Sheet1").Rows("1:2").Copy (Sheets("Sheet2").Rows("6:7"))

Example 3: copy row and paste to new inserted row

ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert

Example 4: copy column

The below code copy Sheet1 column A:B to Sheet2 column B:C (because it pastes at B1).

Sheets("Sheet1").Columns("A:B").Copy (Sheets("Sheet2").Range("B1"))

Example 5: copy multiple Range to a new Workbook

In case the copied Columns are not adjacent to each other, Set a Range to combine those Columns (or Rows).

The below code copy range1 and then paste to A1 of new workbook.

Sub CopyToNewWorkbook()
     Set range1 = Range("A:B, E:F")
     range1.Copy
     Set newbook = Workbooks.Add
     Range("A1").PasteSpecial
 End Sub

Example 6: destination argument omitted

The following code example inspects the value in column D for each row on Sheet1. If the value in column D equals A, the entire row is copied onto SheetA in the next empty row. If the value equals B, the row is copied onto SheetB.

Sub CopyRows()
    Sheets("Sheet1").Select
    ' Find the last row of data
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    ' Loop through each row
    For i = 2 To FinalRow
        ' Decide if to copy based on column D
        ThisValue = Cells(i, 4).Value
        If ThisValue = "A" Then
            Cells(i, 1).Resize(1, 33).Copy
            Sheets("SheetA").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Sheet1").Select
        ElseIf ThisValue = "B" Then
            Cells(i, 1).Resize(1, 33).Copy
            Sheets("SheetB").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Sheet1").Select
        End If
    Next i
End Sub

Range.PasteSpecial method

When working with your spreadsheet, you likely often have to copy formulas and paste them as values. To do this in a macro, you can use the PasteSpecial method.

Syntax

expression.PasteSpecial (PasteOperationSkipBlanksTranspose)

It returns Variant.

Parameters

Name Required/Optional Data type Description
Paste Optional XlPasteType Specifies the part of the range to be pasted.
Operation Optional XlPasteSpecialOperation Specifies how numeric data will be calculated with the destinations cells on the worksheet.
SkipBlanks Optional Variant True to have blank cells in the range on the clipboard not be pasted into the destination range. The default value is False.
Transpose Optional Variant True to transpose rows and columns when the range is pasted. The default value is False.

XlPasteType enumeration

Name Value Description
xlPasteAll -4104 Everything will be pasted.
xlPasteAllExceptBorders 7 Everything except borders will be pasted.
xlPasteAllMergingConditionalFormats 14 Everything will be pasted and conditional formats will be merged.
xlPasteAllUsingSourceTheme 13 Everything will be pasted using the source theme.
xlPasteColumnWidths 8 Copied column width is pasted.
xlPasteComments -4144 Comments are pasted.
xlPasteFormats -4122 Copied source format is pasted.
xlPasteFormulas -4123 Formulas are pasted.
xlPasteFormulasAndNumberFormats 11 Formulas and Number formats are pasted.
xlPasteValidation 6 Validations are pasted.
xlPasteValues -4163 Values are pasted.
xlPasteValuesAndNumberFormats 12 Values and Number formats are pasted.

XlPasteSpecialOperation enumeration

Name Value Description
xlPasteSpecialOperationAdd 2 Copied data will be added to the value in the destination cell.
xlPasteSpecialOperationDivide 5 Copied data will divide the value in the destination cell.
xlPasteSpecialOperationMultiply 4 Copied data will multiply the value in the destination cell.
xlPasteSpecialOperationNone -4142 No calculation will be done in the paste operation.
xlPasteSpecialOperationSubtract 3 Copied data will be subtracted from the value in the destination cell.

Example 1: paste values

Sub PasteSpecialValues()
    Sheets("Sheet1").Range("A1:D5").Copy
    Sheets("Sheet1").Range("A7:D11").PasteSpecial xlPasteValues
    'Remove the animation around the copied cell
    Application.CutCopyMode = False
End Sub

Note: To remove the animation around the copied cell add below code:

Application.CutCopyMode = False

Example 2: paste formats

Sheets("Sheet1").Range("A1:D5").Copy
Sheets("Sheet1").Range("A7:D11").PasteSpecial xlPasteFormats

Example 3: paste formulas

Sheets("Sheet1").Range("A1:D5").Copy
Sheets("Sheet1").Range("A7:D11").PasteSpecial xlPasteFormulas

Example 4: EntireRow copy and paste

Sub EntireRowCopy()
    'Paste the data of row 3 in row 8
    Worksheets("Sheet1").Activate
    Rows(3).EntireRow.Copy
    'PasteSpecial Method without argument copies the format as well.
    Rows(8).EntireRow.PasteSpecial
End Sub

Example 5: Multiply

This example replaces the data in cells A1:C2 on Sheet1 with the multiply of the existing contents and cells D1 on Sheet1.

1
2
A B C D
1 2 3 3
4 5 6

VBA Code

Sub pasteMultiply()
    Range("D1").Copy  'D1 contains value 3
    Range("A1:C2").PasteSpecial Operation:=xlPasteSpecialOperationMultiply
End Sub

Result

1
2
A B C D
3 6 9 3
12 15 18

Example 6: Add

This example replaces the data in cells D1:D2 on Sheet1 with the sum of the existing contents and cells A1:A2 on Sheet1.

1
2
A B C D
1 2 3 3
4 5 6 3

VBA Code

Sub PasteSpecialOperationAdd()
    With Worksheets("Sheet1")
        .Range("A1:A2").Copy
        .Range("D1:D2").PasteSpecial _
         Operation:=xlPasteSpecialOperationAdd
    End With
End Sub

Result

1
2
A B C D
1 2 3 4
4 5 6 7

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>

10 comments
  1. MI
    Mike

    Hi, I am trying to copy rows based on an ID and have never worked with VBA before. So far the code finds and copies the correct rows but when copying, it references to wrong cells. When copying the rows manually, the cells are correctly referenced. That is why I want to try copying with copy and paste special. However, it seems I can't simply change .Formula with .Copy and .PasteSpecial. Can you help? I referenced where I got the original code that I adapted (hope that is ok).

    Sub CopyRow1()
    'https://stackoverflow.com/questions/68927138/copy-rows-from-one-worksheet-to-another-based-on-a-unique-id-colum

    Dim ws As Worksheet
    Dim ws2 As Worksheet

    Set ws = Worksheets("Tabelle2")
    Set ws2 = Worksheets("Tabelle6Liste")

    Dim a As String

    For i = 2 To ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
    a = ws2.Cells(i, 1)

    For j = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    If ws.Cells(j, 1) = a Then

    ws.Cells(j, 2).Formula = ws2.Cells(i, 2).Formula
    ws.Cells(j, 3).Formula = ws2.Cells(i, 3).Formula
    ws.Cells(j, 4).Formula = ws2.Cells(i, 4).Formula
    ws.Cells(j, 5).Formula = ws2.Cells(i, 5).Formula
    ws.Cells(j, 6).Formula = ws2.Cells(i, 6).Formula
    ws.Cells(j, 7).Formula = ws2.Cells(i, 7).Formula
    ws.Cells(j, 8).Formula = ws2.Cells(i, 8).Formula
    ws.Cells(j, 9).Formula = ws2.Cells(i, 9).Formula
    ws.Cells(j, 10) = ws2.Cells(i, 10)
    ws.Cells(j, 11) = ws2.Cells(i, 11)
    ws.Cells(j, 12) = ws2.Cells(i, 12)
    ws.Cells(j, 13).Formula = ws2.Cells(i, 13).Formula
    ws.Cells(j, 14).Formula = ws2.Cells(i, 14).Formula
    ws.Cells(j, 15).Formula = ws2.Cells(i, 15).Formula
    ws.Cells(j, 16) = ws2.Cells(i, 16)
    ws.Cells(j, 17) = ws2.Cells(i, 17)
    ws.Cells(j, 18) = ws2.Cells(i, 18)

    End If
    Next j

    Next i
    End Sub

  2. DA
    Dave

    Hi -
    I'm sure this is a simple task, but I'm trying to figure out how to copy/paste a range based on a different value. So basically, if the value in cell D2 is "1", then copy range I10:L10 and paste it in cell I25:L25. If D2="2" then copy range I11:L11 and paste it in cell I25:L25. I'm trying to work through this simple "If-Then" statement as I will ultimately apply it to another application. I have written the following code, but it is not working for me. Each of my "Copy" macros work individually, but when trying the If-then it does not work as intended. Any suggestions on how to fix this?

    Sub Copy_Paste_Macro_RUN()

    If D4 = "1" Then
    Call Copy1
    ElseIf D4 = "2" Then
    Call Copy2
    ElseIf D4 = "3" Then
    Call Copy3
    End If

    End Sub

    • DA
      Dave

      @Dave I meant the value in D4 (not D2) in the above example. Thanks!

      • EX

        @Dave

        Sub Copy_Paste_Macro_RUN()
            If Range("D4") = 1 Then
                Call Copy1
            ElseIf Range("D4") = 2 Then
                Call Copy2
            ElseIf Range("D4") = 3 Then
                Call Copy3
            End If
        End Sub
        Sub Copy1()
            Range("I10:L10").Copy Range("I25:L25")
        End Sub
        Sub Copy2()
            Range("I11:L11").Copy Range("I25:L25")
        End Sub
        Sub Copy3()
            Range("I12:L12").Copy Range("I25:L25")
        End Sub
  3. SM
    Sam Molli

    Hi there
    Please help me to write a VBA command to copy the text on Cell X and paste it on the "cell reference" on Cell Y.
    ie. Cell Y has the "cell reference B4", I wand to write a command to copy Cell X by piking the location from Cell Y and paste it on Cell B4. (paste location given on Cell Y - would be changing as needed)
    Thank you for your help
    Sam

    • EX

      @Sam Molli

      Private Sub Worksheet_Change(ByVal Target As Range)
      'replace $E$2 with Cell Y, A1 with Cell X.
      'your Cell Y's value must like "B2"
          If Target.Address = "$E$2" Then
               Range(Target.Value) = Range("A1")
          End If
      End Sub
More comments