Sorting Excel Worksheets using Testcomplete scripts:
Well, I got it working by creating an Excel macro enabled Template and creating the VBScript:
Cells.Select
ActiveWorkbook.Worksheets("{worksheet}").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("{Worksheet}").Sort.SortFields.Add Key:= _
Range("A2:A200"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("{worksheet}").Sort
.SetRange Range("A1:D200")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
where {worksheet} is the excel worksheet name.
Change the Range to the column(s) you want sorted.
Then envoke the macro via TC script (VB script):
Set Excel = Sys.OleObject("Excel.Application")
Excel.Workbooks.Open(filepath & filename)
' Excel.Visible = "True" ' enable if you want Excel to open on desktop
Excel.Run("{Macro Name}")
Excel.Quit
I have additional Excel macro code to import the data from a csv file to be sorted and edited, then saves the file with a new name, then deletes the imported data in the Excel template, resaves the blank template.
What would normally take me a couple of minutes to launch Excel, import test data in csv format, sort it, perform other formating to the data, then resave the data in a new xls format file now takes less than 5 seconds between the Excel macro and the TC script.
Note: I found that it is possible to do what I needed via TestComplete scripts, but after numerous headaches trying to code using the Excel COM objects, I decided to do everything in an Excel Macro. Much easier and much faster.