按三个单独的列对数据进行排序

时间:2014-02-11 21:52:10

标签: excel vba

我正在尝试按电子表格中的三个不同值对数据进行排序:

  1. F栏 - 值 - 最旧到最新
  2. B栏 - 值 - 最小到最大
  3. A栏 - 值 - Z到A
  4. 代码在 .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
    

1 个答案:

答案 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