范围类的排序方法失败运行时错误 1004



当我尝试对脚本中的列进行排序时,它返回错误范围类的排序方法失败运行时错误 1004。下面我包含了调试此错误的脚本以及脚本中使用的声明。

Set SbA = Worksheets("SumByAddress")
Set SbM = Worksheets("SumByMeterSize")
Set SFD = Worksheets("ShortFormData")
Counter = 2
premiseID = ""
RowNew = 2
TotalRows = SFD.Cells(1, 1).End(xlDown).Row
Application.Calculation = xlCalculationManual
TimeStart = Now
PrevUpdate = Now
SFD.Range("M15") = "Creating summation by address"
Application.ScreenUpdating = False
SFD.Range("A:I").Sort , _
key2:=Range("A2"), order2:=xlAscending, Header:=xlYes, MatchCase:=True, _
key3:=Range("D2"), order3:=xlAscending, Header:=xlYes, MatchCase:=True

您需要先指定 Key1,然后再指定 Key2 和 Key3。例如:

Private Sub sortSheet(ByRef sh As Excel.Worksheet, ByVal rc As String)
Dim lastRow As Long
lastRow = sh.Cells(sh.rows.count, "A").End(xlUp).row
With sh.Range("A1:" + rc + CStr(lastRow))
.Sort Key1:=sh.Range("B1"), Order1:=xlAscending, _
Key2:=sh.Range("A1"), Order2:=xlAscending, _
Header:=xlYes, _
MatchCase:=False, _
Orientation:=xlTopToBottom
End With
End Sub

此外,HeaderMatchCase只应指定一次。

最新更新