Sunday, February 8, 2009

Excel VBA Macro Example: Copying a range

In this Excel VBA Macro Example section, I will show you how to copy a range from C4:E4 to G10:H10.


Sub CopyRange()

   Sheets("Sheet1").Range("C4:E4").Copy _
     Destination:=Sheets("Sheet1").Range("G10")

End Sub

If Destination argument is omitted, Microsoft Excel copies the range to the Clipboard.

If you only want to copies value of the range (simulate copy paste specials value), you may use the following code:


Sub CopyPasteValue()

   Sheets("Sheet1").Range("C4:E4").Copy
   Sheets("Sheet1").Range("G10").PasteSpecial _
     Paste:=xlPasteValues
End Sub

No comments:

Post a Comment