当我尝试对脚本中的列进行排序时,它将返回错误Range类的Sort方法失败,运行时错误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
答案 0 :(得分:2)
您需要先指定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
此外,Header
和MatchCase
仅应指定一次。