Friday, April 2, 2010

VBScript - Appending Data to an Excel Spreadsheet

VBScript - Appending Data to an Excel Spreadsheet

All we have to do is determine the bottom row in the used range; the next available row for data entry will then be the row immediately following the used range. That’s exactly how this script operates:

Const xlCellTypeLastCell = 11

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

objWorksheet.Cells(1,1) = 1
objWorksheet.Cells(2,1) = 2
objWorksheet.Cells(3,1) = 3
objWorksheet.Cells(5,1) = 4
objWorksheet.Cells(6,1) = 5

Set objRange = objWorksheet.UsedRange
objRange.SpecialCells(xlCellTypeLastCell).Activate

intNewRow = objExcel.ActiveCell.Row + 1
strNewCell = "A" & intNewRow
objExcel.Range(strNewCell).Activate

[Via]