WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Excel
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Delete Column

 Keywords: remove delte column columns Excel XLS 

Question:

I need help deleting some columns of data in an Excel workbook... I have my code working but there are some columns of data that are not need and I would like to remove them. I am not sure how to do this.

Any help or examples would be appreciated

Answer:

Here is the code I would use to delete a single column regardless of how many rows it contains:
column = "B:B" ;Column to delete

;Define .XLS file
;xls = "C:\Temp\Data\test.xls"
title = "Excel Files"
path = DirScript()
mask = "XLS Files (*.xls)|*.xls"
xls = AskFilename(title, path, mask, "", 1)

objXL = ObjectCreate("Excel.Application")
objXL.Visible = @TRUE
objXL.DisplayAlerts = @FALSE

;Open XLS file
objWorkbooks = objXL.Workbooks
objWorkbooks.Open(XLS)

;Get Active WorkSheet Object
objActiveSheet = objXL.ActiveSheet
If objActiveSheet == 0
   Message("","No active workSheet")
   objActiveWorkbook = 0
   objXL = 0
   Exit
EndIf
wrkshtname = objActiveSheet.Name
;Message("Active WorkSheet Name",wrkshtname)

objWorkSheet = objXL.Worksheets(wrkshtname)
objcols = objWorkSheet.Columns(column)
objcols.Select

;Delete Column
objcols.Delete()


objWorkbooks.Close()
objXL.Quit()
objcols = 0
objWorkSheet = 0
objActiveSheet = 0
objWorkbooks = 0
objXL = 0
Exit

Article ID:   W17449
File Created: 2008:04:10:15:10:36
Last Updated: 2008:04:10:15:10:36