|Excel And SQL .com|
Home > VBA Macros
Copy Range and Paste Values using VBA
One of the actions users perform most often when using excel is to cut and paste cells from one area to another.
Excel's cut and paste is so advanced, it gives you the user so many features, like just copying the range of cells with all their formating, formulas etc or just copy only values.
All these advanced copying features can also be implemented using VBA. Below is the snippet of code that demonstrates these features using code examples.
Sub Demo() 'initialize some cells with test data and formating. Range("B3:B6") = "ZZZZ" Range("B3:B6").Interior.Color = vbYellow 'Copy All Range("B3:B6").Copy Destination:=Range("C3") Range("B3:B6").Copy Destination:=Sheets("Sheet1").Range("D3") 'Copy only values Range("B3:B6").Copy Range("E3").PasteSpecial xlPasteValues Range("B3:B6").Copy 'Copy just the formating of the cell Range("F3").PasteSpecial xlPasteFormats 'Copy column widths Range("F3").PasteSpecial xlPasteColumnWidths End Sub
|© All Rights Reserved. ExcelAndSql.com|