4

we are generating an Excel report and we need to sort the data programmatically before saving the file.

Is it possible to sort a specific column of an Excel file programmatically using vb.net?

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

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

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....

gobjExcelReportSheet.Application.Selection.Autofilter()

But there is no option for sorting programmetically.

Please help me...

Thanks!

1
  • Is the report being generated by using the Office Interop Assemblies? If you give a code example of what you are doing you are much more likely to get a valuable response. Commented Feb 18, 2009 at 11:53

1 Answer 1

8

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

Dim myWorkSheet As Excel.Worksheet = myWorkbook.Worksheets(aSheetName)
myWorkSheet.Activate()
Dim myRange As Excel.Range

myRange = myWorkSheet.Range("A1", "L10")
myRange.Select()


myRange.Sort(Key1:=myRange.Range("D1"), _
                        Order1:=Excel.XlSortOrder.xlAscending, _
                        Orientation:=Excel.XlSortOrientation.xlSortColumns)

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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.
Good. An upvote and/or an accepted answer (hint, click on the tick) would be nice then? ;)
Thank you. It's not really about the rep it's that it helps everyone know whether or not it's the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.