Pages

Wednesday, March 2

Working with Excel Sheets - create, open, modify, save, close!!

'Getting value from an existing excel sheet

Dim oExcel, oWB, oSheet, getVal
Set oExcel=CreateObject("Excel.Application")
Set oWB=oExcel.Workbooks.Open("C:\abhikansh.xls")
Set oSheet=oWB.WorkSheets("Sheet1")
oExcel.Visible=TRUE
getVal=oSheet.Cells(1,1).Value
print getVal
oWB.Close
Set oExcel=Nothing


'Inserting value to an existing excel sheet
Dim oExcel, oWB, oSheet, setVal
Set oExcel=CreateObject("Excel.Application")
Set oWB=oExcel.Workbooks.Open("C:\abhikansh.xls")
Set oSheet=oWB.WorkSheets("Sheet1")
oExcel.Visible=TRUE
setVal="ValueInserted"
oSheet.Cells(4,1).Value=setVal

oWB.Save
oWB.Close
Set oExcel=Nothing


'Create a new excel sheet, insert values and save it..
Dim oExcel, oWB, oSheet, setVal
Set oExcel=CreateObject("Excel.Application")
Set oWB=oExcel.Workbooks.Add
Set oSheet=oWB.WorkSheets("Sheet1")
oExcel.Visible=TRUE
setVal="ValueInserted"
oSheet.Cells(4,1).Value=setVal

oWB.SaveAs("C:\abhikansh1.xls")
oWB.Close
Set oExcel=Nothing