我正在尝试按电子表格中的三个不同值对数据进行排序:
代码在 .Apply 部分停止并显示错误。我担心细胞选择是个问题。
Sub DatePartOrderSort()
'
' DatePartOrderSort Macro
'
'
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range(Cells(5, "A"), Cells(LR, "J")).Select
'
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
Cells(5, "A"), Cells(LR, "J")), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
Cells(5, "A"), Cells(LR, "J")), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
Cells(5, "A"), Cells(LR, "J")), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Date Order").Sort
.SetRange Range(Cells(5, "A"), Cells(LR, "J"))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
答案 0 :(得分:2)
试试这个:
Sub DatePartOrderSort()
'
' DatePartOrderSort Macro
'
'
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range(Cells(5, "A"), Cells(LR, "J")).Select
'
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
"F5:F" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
"B5:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
"A5:A" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Date Order").Sort
.SetRange Range(Cells(5, "A"), Cells(LR, "J"))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub