Friday, May 21, 2010

How to add a sheet in Excel file through VB Scripting.

Please go through the below code. Which opens a excel file, adding a sheet with a new name and saving the file in the desired location.

Set obj_ExcelSheet = CreateObject("Excel.Application")' creating the excel object
obj_ExcelSheet.visible = True
obj_ExcelSheet.SheetsInNewWorkbook = 1 ' no of sheets to be added
obj_ExcelSheet.Workbooks.Add() ' actually adding the sheet to the file obj_ExcelSheet.Worksheets(1).Activate ' make the sheet no-1 activate
obj_ExcelSheet.Activesheet.Name = "Check" 'name of the sheet
obj_ExcelSheet.ActiveWorkbook.saveas ("C:\Documents and Settings\username\Desktop\MF.xls") 'saving the file in desired location.
obj_ExcelSheet.quit ' quiting from the excel.
Set obj_ExcelSheet = nothing

Please provide your feedback on the same.
Reblog this post [with Zemanta]

No comments:

Post a Comment