Monday, April 25, 2011

How to do filter (sorting) on Columns of excel file programmetically using

we are generating a excel report and we need to sort the data programmatically before saving the file.

Is it possible to implement filter on a specific column i.e. sorting of a excel file programmatically using

As per your suggetion, in order to generate excel report with sorting on specific column I just implemented the logic as below..

Dim MyRange As Excel.Range
      MyRange = gobjExcelReportSheet.Range("A8", "L8")
      MyRange.Sort(Key1:=MyRange.Range("L8"), _
                   Order1:=XlSortOrder.xlAscending, _
                   Header:=XlYesNoGuess.xlGuess, _
                   MatchCase:=False, _

I tried this logic just before saving the file and even just after saving the file but it is not giving any result i.e. with sorting result or even I am not getting any error.

But the following code works....


But there is no option for sorting programmetically.

Please help me...


From stackoverflow
  • Assuming you're using the interop assemblies this is how I do it:

    Dim myWorkSheet As Excel.Worksheet = myWorkbook.Worksheets(aSheetName)
    Dim myRange As Excel.Range
    myRange = myWorkSheet.Range("A1", "L10")
    myRange.Sort(Key1:=myRange.Range("D1"), _
                            Order1:=Excel.XlSortOrder.xlAscending, _

    Basically you need to select a range (in this case A1:L10, then choose to sort it by a certain column (D).

    Suman : I have implemented this, but I am getting expected result even not getting any error. Do I need to do any other settings? Please suggest. Thank you.
    RobS : I take it you mean you're not getting the expected result? If so, then we need much more information. Please edit your question showing what you've done so far.
    Suman : Hi All, Now its working with following code.... MyRange.Range("A8").Sort(Key1:=MyRange.Range("L8"), Order1:=XlSortOrder.xlAscending, Header:=XlYesNoGuess.xlGuess, MatchCase:=False, Orientation:=XlSortOrientation.xlSortColumns) Along with other lines as mentioned above. Thank you.
    RobS : Good. An upvote and/or an accepted answer (hint, click on the tick) would be nice then? ;)
    Remou : I agree, an upvote, at the very least, is a good idea.
    Remou : .. so +1 from me.
    RobS : Thank you. It's not really about the rep it's that it helps everyone know whether or not it's the correct answer.


Post a Comment