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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21713834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:52:58  来源:igfitidea点击:

Sorting data by three separate columns

excelvba

提问by TkdKidSnake

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

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

  1. Column F - Values - Oldest to Newest
  2. Column B - Values - Smallest to Largest
  3. Column A - Values - Z to A
  1. F 列 - 值 - 最旧到最新
  2. B 列 - 值 - 最小到最大
  3. A 列 - 值 - Z 到 A

The code stops with an error on the .Applysection. 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

采纳答案by teylyn

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