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

时间:2022-12-03 06:11:25

I am trying to sort data in a spreadsheet by three different values:

我试图通过三个不同的值对电子表格中的数据进行排序:

  1. Column F - Values - Oldest to Newest
  2. F列 - 值 - 从最新到最新
  3. Column B - Values - Smallest to Largest
  4. B栏 - 值 - 最小到最大
  5. Column A - Values - Z to A
  6. A列 - 值 - Z到A.

The code stops with an error on the .Apply section. I fear the cell selection is the issue.

代码在.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 个解决方案

#1


2  

Try this:

尝试这个:

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

#1


2  

Try this:

尝试这个:

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