Delete Rows From excel sheet

Share this post
FaceBook  Twitter  Mixx.mn     

To Delete Rows from a Excel sheet, use following code:
Steps:
1) Create a Excel application object

2) Open Excel file

3) Activate the sheet

4) Delete Rows range

5) Save the Excel file

6) close the Excel file


Code:

Dim StartRow,EndRow,DestPath,SrcPath
SrcPath="C\........\input.xls"
StartRow=1
EndRow=20
DestPath="C\.....\abc.xls"

'1) Create a Excel application object'
Set oExcel = CreateObject("Excel.Application")
'Sets the application to raise no app alerts
'In this case it will allow a file overwrite w/o raising a 'yes/no’ dialog
oExcel.DisplayAlerts = False'Open Book in Excel

'2) Open Excel file'
Set oBook = oExcel.Workbooks.Open(SrcPath)

'3) Activate the sheet
Set oSheet = oExcel.Activesheet

'4) Delete Rows range
oSheet.Rows(StartRow + ":" + EndRow).Delete

'5) Save the Excel file
oBook.SaveAs (DestPath)

'6) close the Excel file'
oExcel.Workbooks.Close()