vba C# Excel 范围排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6706702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Excel Range Sort
提问by Joe.Net
I would like to sort a range. The first row (Row 3 in the Excel workbook) contains the column headers, which need sorted, left to right, in ascending order:
我想对一个范围进行排序。第一行(Excel 工作簿中的第 3 行)包含列标题,需要按升序从左到右排序:
Excel.Range tempRange = ws.get_Range("F3", "H8");
tempRange.Sort(Type.Missing,
Excel.XlSortOrder.xlAscending,
Type.Missing,
Excel.XlSortOrder.xlAscending,
Excel.XlSortOrder.xlAscending,
Type.Missing,
Excel.XlSortOrder.xlAscending,
Excel.XlYesNoGuess.xlYes,
Type.Missing,
Type.Missing,
Excel.XlSortOrientation.xlSortColumns,
Excel.XlSortMethod.xlPinYin,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal);
This currently generates the error 'Sort method of Range class failed'.
这当前会生成错误“范围类的排序方法失败”。
I've tried various parameters at the start of the sort method, but this generates the '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' error message.
我在 sort 方法开始时尝试了各种参数,但这会生成“排序引用无效”。确保它在您要排序的数据内,并且第一个“排序依据”框与“不同”或“空白”错误消息。
Where am I going wrong?
我哪里错了?
The equivalent VBA Works fine :
等效的 VBA 工作正常:
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("F3:H8")
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Thanks very much
非常感谢
Joe
乔
回答by Joe.Net
I stated the Range as the first parameter & set the Orientation to Excel.XlSortOrientation.xlSortRows.
我将范围声明为第一个参数并将方向设置为 Excel.XlSortOrientation.xlSortRows。
tempRange.Sort(tempRange,
Excel.XlSortOrder.xlAscending,
Type.Missing, Type.Missing,
Excel.XlSortOrder.xlAscending,
Type.Missing,
Excel.XlSortOrder.xlAscending,
Excel.XlYesNoGuess.xlYes,
Type.Missing,
Type.Missing,
Excel.XlSortOrientation.xlSortRows,
Excel.XlSortMethod.xlPinYin,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal);
Useful link :
有用的链接:
http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/a699d754-98d5-4241-87da-8761c520ba72/
http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/a699d754-98d5-4241-87da-8761c520ba72/