Friday, April 9, 2010

VBScript - How Can I Insert a Column into a Spreadsheet?

VBScript - How Can I Insert a Column into a Spreadsheet?

Here's a script that writes a column header to columns A, B, and C, then inserts a new, blank column between columns B and C:

Const xlShiftToRight = -4161

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1,1) = "Dataset 1"
objWorksheet.Cells(1,2) = "Dataset 2"
objWorksheet.Cells(1,3) = "Dataset 4"

Set objRange = objExcel.Range("C1").EntireColumn
objRange.Insert(xlShiftToRight)

[Via]