vba VB.net Excel排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2087726/
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
VB.net Excel sorting
提问by Lora
I am trying to get a macro convert from VBA over to vb.net and I am getting a type mismatched error and can't figure it out. I am hoping someone here will be able to help me.
我正在尝试将宏从 VBA 转换为 vb.net,但出现类型不匹配的错误,无法弄清楚。我希望这里有人能够帮助我。
This is the code.
这是代码。
Sub SortRawData()
Dim oSheet As Excel.Worksheet
Dim oRange As Excel.Range
Try
oSheet = SetActiveSheet(mLocalDocument, "Sheet 1")
oRange = mApplication.ActiveSheet.UsedRange
oRange.Sort(Key1:=oRange("J2"), Order1:=Excel.XlSortOrder.xlAscending, _
Header:=Excel.XlYesNoGuess.xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=Excel.XlSortOrientation.xlSortColumns, _
DataOption1:=Excel.XlSortDataOption.xlSortNormal, _
DataOption2:=Excel.XlSortDataOption.xlSortNormal, _
DataOption3:=Excel.XlSortDataOption.xlSortNormal)
Catch ex As Exception
ErrorHandler.HandleError(ex.Message, ex.Source, ex.StackTrace)
End Try
End Sub
This is the code from the macro
这是宏的代码
Sub SortRawData(ByRef poRange As Range)
Set poRange = Application.ActiveSheet.UsedRange
poRange.Sort Key1:=Range("J2"), Order1:=xlAscending _
, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal
poRange.Sort Key1:=Range("D2"), Order1:=xlAscending, _
Key2:=Range("H2"), Order2:=xlAscending, _
Key3:=Range("L2"), Order3:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal
End Sub
Any help would be appreciated.
任何帮助,将不胜感激。
Thanks!
谢谢!
回答by henryt1975
Simple change the first line like this: oRange.Sort(Order1:=Excel.XlSortOrder.xlAscending, Key1:=oRange("J2")_
像这样简单地更改第一行: oRange.Sort(Order1:=Excel.XlSortOrder.xlAscending, Key1:=oRange("J2")_