Monday, October 8, 2012

Working with Excel



'Open existing excel file
Set obj=Createobject("Excel.Application")
Set objwb=obj.WorkBooks.Open("D:\temp1.xls")
Set objsheet=objwb.Sheets("Sheet1")
obj.visible="True"


'Delete Row from sheet
objsheet.Rows(4).Delete


'Delete Columns from sheet
objsheet.Columns(1).Delete


'Enter data into excel sheetobjsheet.cells(5,5)="Welcome"


'Change Font Color
objsheet.Cells(5,5).Font.Color = vbRed


'Get data from excelsheet by mentioning row and col
msgbox objsheet.cells(5,1)


'Change name of excel sheet
objsheet.Name ="NewSheet"


'Count # of sheet
sheetCount = objwb.Sheets.Count


'Add New Sheet
objwb.Sheets.Add


'Delete existing sheet
objsheet.Delete
RemoveWorksheet = "SheetNew"


'Find/Search data in excel Sheet   If search_item found within excel sheet , flag will hold the row # (found)
search_item="North"
set foundrow=objsheet.Cells.Find(search_item)
On Error resume next
 flag=foundrow.Row
msgbox flag
objwb.Save
objwb.Close