Pages

Thursday, March 3

Working with Excel Sheets - Add new sheet

'This code demonstrate how to add new sheet to existing excel file and rename it at runtime

Dim oExcel, oWB, oSheet, getVal
Set oExcel=CreateObject("Excel.Application")
oExcel.visible=True
oExcel.DisplayAlerts = False   'this will not allow alerts to be displayed
Set oWB=oExcel.Workbooks.Open("C:\abhikansh.xls")
Set oSheet1=oWB.Sheets.Add  'a new sheet is added with default name i.e. Sheet4 etc.

oSheet1.Name="QTPSchools"  'rename newly added sheet
oSheet1.cells(1,1).value="Welcome"  'enter value
oWB.Save
oWB.Close
Set oExcel=Nothing