Forum Discussion

amit_1's avatar
amit_1
New Contributor
11 years ago

How to sort excel sheet column wise?



Hi,



I need to sort an excel sheet, column wise through C++ Script.

I am not able to find anything regarding that on Test Complete help.



Kindly help me with the same.



I am currently using Test Complete version 6.



Thanks and Regards,

Amit Tripathi

6 Replies

  • hautzenroeder1's avatar
    hautzenroeder1
    Occasional Contributor
    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.


     






  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Amit,


     


    That's a very interesting task. Is there anybody who tried created a script for this?


    I'm not sure whether it's possible to accomplish it via Excel's COM object.


     

  • hautzenroeder1's avatar
    hautzenroeder1
    Occasional Contributor
    Well...  I was able to sort a worksheet by recording a script and manually performing all the steps needed as one of my test scripts requires a sorted Excel spreadsheet.



    However, this returns a code that uses absolute mouse position on the open Excel worksheet and running a test with a different sized Excel window causes the mouse position to be different.

    But as I run this script with the Excel window maximized using the same monitor, it works.



    Another Solution is to record a Macro of what is needed, then use:



    excel.Run("MACRO_NAME")



    to run the macro.



    I haven't tried this method, yet. So I'm not sure just how well it'll work.

    It's on my ToDo list
  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi John,


     


    Thanks for your feedback! Using Excel macros can do the trick.


    Let us know if you have any questions when creating it - we will try to help.