locked
Sorting Excel Column Using C# RRS feed

  • Question

  • I have a excel file and I am trying to export data from my program to my excel file. Everything else seems to work. However there is a column (4) in my excel file that isn't sort properly. In that column, I have a column of number: 42, 12, 53, 32, 22, 100, 23. I want to sort it like 12, 22, 32, 42, 53, 22, 100, 23. Here is my c# code

     //Student Sheet Auto Fit
                        dynamic bRange = (Excel.Range)xlStudentDetail.get_Range("A1", "M1");
                        bRange.EntireColumn.AutoFit();
                        bRange.Sort(bRange.Columns[4], Excel.XlSortOrder.xlAscending);

    Help will be appreciate, thanks  

    Tuesday, September 29, 2015 8:54 AM

Answers

All replies


  • Re:  custom column sort

    I don't speak C#, but will stick my head out anyway...
    1.  In VBA... Range("A1", "M1") ... would be a row not a column.
    2.  Your sort preference doesn't seem to follow any logical sequence.
         Excel does offer custom sort sequences that you can setup (in VBA) using the "AddCustomList" method.
    '---

    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel add-ins & workbooks)

    • Edited by James Cone Monday, October 31, 2016 1:54 AM
    Tuesday, September 29, 2015 3:46 PM
  • Hi,

    Please refer to these steps below to achieve that:

    1. Clear sort fields
    2. Add sort field
    3. Set sort range and method
    4. Apply sort

    There is a similar thread that can help you:

    # Sorting Columns in Excel C#

    https://social.msdn.microsoft.com/Forums/office/en-US/27a14605-32f6-4fe6-8e59-b5c40de1e6a7/sorting-columns-in-excel-c?forum=exceldev

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 30, 2015 9:43 AM