Friday, April 2, 2010

Excel: Copy And Paste Cells/Rows/Columns Using JScript/VBScript

Excel: Copy And Paste Cells/Rows/Columns Using JScript/VBScript

' Example 1, the whole contents of the work sheet
dim wsA,wsB
set wsA=wbA.WorkSheets.item("Sheet1")
set wsB=wbB.WorkSheets.item("Sheet2")

' This is critical, you cannot copy from a non active sheet
wsA.Activate
wsA.Cells.Select
excel.Selection.Copy
wsB.Activate
wsTo.Paste

' Example 2, just the second row, to the third row
wsA.Activate
wsA.Rows.item(2).Select
excel.Selection.Copy
wsB.Activate
wsTo.Rows(3).Select
wsB.Paste

' Example 3, just the second column, to the third column
wsA.Activate
wsA.Columns.item(2).Select
excel.Selection.Copy
wsB.Activate
wsTo.Columns(3).Select
wsB.Paste

' Example 4, just the cell A1, to cell B2
wsA.Activate
wsA.Range('A1').Select
excel.Selection.Copy
wsB.Activate
wsTo.Range('B2').Select
wsB.Paste

[Via]