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.

Sort Columns in XLS

Keywords: 	  sort columns Excel XLS

Question:

Once a XLS file is opened in OLE, is there a way to highlight all columns and sort the content on columns C, G and B ?

Answer:

Your best bet is to populate your Excel sheet then open it, and record a macro of the sort process. Edit the macro, and recode it in Winbatch.

Here is an example of sorting data that contains a header row. It sorts the data in Column C in ascending order.

file = "C:\Temp\Regmon-Failed.LOG"
xlAscending = 1
xlYes =1
xlTopToBottom = 1
xlSortNormal = 0

objExcel = ObjectCreate("Excel.Application")
objExcel.DisplayAlerts = @FALSE
objExcel.visible = @TRUE

objExcel.WorkBooks.Open(file)
objWork_Sheet =  objExcel.Activeworkbook.ActiveSheet
objWork_Sheet.Columns("C:C").Select
objWork_Sheet.Range("A1:E666").sort(:: Key1=objWork_Sheet.Range("c1"), Order1=xlAscending, Header=xlYes, OrderCustom=1, MatchCase=@FALSE, Orientation=xlTopToBottom, DataOption1=xlSortNormal )

Pause("Notice","Sorted Ascending based on Column C")

objRangecol = 0
objRange = 0
objWork_Sheet = 0
objExcel.WorkBooks.Close()
objExcel.Quit()
objWork_Sheet = 0
objExcel = 0

Article ID:   W15258
File Created: 2007:02:07:12:14:30
Last Updated: 2007:02:07:12:14:30