The Copy and Paste methods

You can perform copy and paste operations in VBA by using the Copy and Paste methods. The Copy method is applicable to the Range object, but the Paste method is applicable to the Worksheet object. This short macro copies range A1:A12 and pastes it to the range beginning at cell C1:

Sub

CopyRange()

Range("A1:A12").Select

Selection.Copy

Range("C1").Select

ActiveSheet.Paste

End

Sub

Notice that in the preceding example, which the macro recorder generated, the ActiveSheet object is used with the Paste method. This is a special version of the Worksheet object that refers to the presently selected worksheet. Also notice that the macro selects the range before copying it. However, you don't have to select a range before doing something with it. In fact, the following procedure accomplishes the same task as the preceding example by using a single statement:

Sub CopyRange2()

Range("A1:A12").Copy Range("C1") End Sub

This procedure takes advantage of the fact that the Copy method can use an argument that corresponds to the destination range for the copy operation.

0 0

Post a comment