开发者

VB.net Excel sorting

开发者 https://www.devze.com 2022-12-17 05:46 出处:网络
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.

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.

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!


Simple change the first line like this: oRange.Sort(Order1:=Excel.XlSortOrder.xlAscending, Key1:=oRange("J2")_

0

精彩评论

暂无评论...
验证码 换一张
取 消