按三个单独的列对数据进行排序 [英] Sorting data by three separate columns

查看:100
本文介绍了按三个单独的列对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. F列-值-从最早到最新
  2. B列-值-最小到最大
  3. A列-值-Z到A

该代码在 .Apply 部分出现错误时停止.我担心选择单元格是个问题.

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

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

推荐答案

尝试一下:

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

这篇关于按三个单独的列对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆