Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai friends I need to sort selected range in excel, I workout this in VBA macro it works, but I convert it to c# it throw the exception.

the Exception message shows
The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.

VBA CODING:
VB
Range("C232:C342").Select
ActiveWorkbook.Worksheets("Headword_list").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Headword_list").Sort.SortFields.Add Key:=Range("C2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Headword_list").Sort
     .SetRange Range("A232:M342")
     .Header = xlNo
     .MatchCase = False
     .Orientation = xlTopToBottom
     .SortMethod = xlPinYin
     .Apply
End With

my C# code:
C#
xlWorkSheet.Sort.SortFields.Add(bcrRange[rcnt, 3], Excel.XlSortOn.xlSortOnValues, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortDataOption.xlSortNormal);

xlWorkSheet.Sort.SetRange(bcrRange);
xlWorkSheet.Sort.MatchCase = false;
xlWorkSheet.Sort.Header = Excel.XlYesNoGuess.xlNo;
xlWorkSheet.Sort.Orientation = Excel.XlSortOrientation.xlSortRows;
xlWorkSheet.Sort.SortMethod = Excel.XlSortMethod.xlPinYin;
xlWorkSheet.Sort.Apply();

how to solve it I need to sort the excel selected range in ascending order based on column 3. any one have solution?
Posted
Updated 18-May-15 21:20pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900