应用程序或对象定义错误。运行时错误1004

时间:2015-07-14 06:39:43

标签: excel

Cells.Sort Key1:=Range(rng1), Order1:=xlAscending, Key2:=Range(rng2) _
  , Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
  False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, DataOption2 _
  :=xlSortNormal

它抛出了一个名为应用程序错误或对象定义错误的错误。

1 个答案:

答案 0 :(得分:0)

尝试此排序操作编码替代方案。它只是削减到你绝对需要的东西。

dim rng1 as range, rng2 as range

With ActiveSheet   '<-set this properly as With Sheets("Sheet1")
    set rng1 = .Range("A1")   'Top cell of the primary sort key
    set rng2 = .Range("B1")   'Top cell of the secondary sort key
    With .Range("A1").CurrentRegion        '<-sort the 'island' of data that surrounds A1 (with a header)
        .Cells.Sort Key1:=rng1, Order1:=xlAscending, _
                    Key2:=rng2, Order2:=xlAscending, _
                    Orientation:=xlTopToBottom, Header:=xlYes
    End With
End With