To Delete Columns from a Excel sheet

Share this post
FaceBook  Twitter  Mixx.mn     

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

2) Open Excel file

3) Activate the sheet

4) Delete Columns

5) Save Excel

6) close Excel file


Code:


Dim StartCol,EndCol,DestPath,SrcPath
SrcPath="C\........\input.xls"
StartCol=1
EndCol=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 Columns
oSheet.Columns(StartCol + ":" + EndCol).Delete

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

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