Excel VBA - 多级排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43074087/
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 12:21:42  来源:igfitidea点击:

Excel VBA - multi level sorting

excelexcel-vbasortingvba

提问by Boosted_d16

How do I change the code below to sort in a multi level way? At present, the code sorts the table one column at a time, I want to sort it together as a multi level sort.

如何更改下面的代码以多级排序?目前代码对表格一次一列进行排序,我想把它作为多级排序一起排序。

Below is what Im trying to achieve:

以下是我试图实现的目标:

enter image description here

在此处输入图片说明

Here's my code which sorts the table one column at a time:

这是我的代码,一次对表格进行一列排序:

Range("A4:L" & lastRow).Sort key1:=Range("A4:A" & lastRow), _
    order1:=xlAscending, Header:=xlNo
Range("A4:L" & lastRow).Sort key1:=Range("B4:B" & lastRow), _
    order1:=xlAscending, Header:=xlNo
Range("A4:L" & lastRow).Sort key1:=Range("C4:C" & lastRow), _
    order1:=xlAscending, Header:=xlNo
Range("A4:L" & lastRow).Sort key1:=Range("D4:D" & lastRow), _
    order1:=xlAscending, Header:=xlNo
Range("A4:L" & lastRow).Sort key1:=Range("E4:E" & lastRow), _
    order1:=xlAscending, Header:=xlNo

How do I change the above to sort everything together?

如何更改上述内容以将所有内容排序在一起?

回答by

I always recommend getting rid of the recorded .Sort method in favor of 'only what you need' VBA Sort code. However, there is a problem in that you can only sort a maximum of three sort keys per sort; the solution is to perform two sort operations. Sort the highest ordinals first then the last three primary sort ordinals.

我总是建议摆脱记录的 .Sort 方法,转而使用“仅您需要的”VBA 排序代码。但是,有一个问题,每次排序最多只能对三个排序键进行排序;解决方案是执行两个排序操作。先对最高序数排序,然后再对最后三个主要排序序数排序。

With Worksheets("Sheet1").Range("A4:L" & lastRow)
    .Cells.Sort Key1:=.Columns("D"), Order1:=xlAscending, _
                Key2:=.Columns("E"), Order2:=xlAscending, _
                Orientation:=xlTopToBottom, Header:=xlYes
    .Cells.Sort Key1:=.Columns("A"), Order1:=xlAscending, _
                Key2:=.Columns("B"), Order2:=xlAscending, _
                Key3:=.Columns("C"), Order3:=xlAscending, _
                Orientation:=xlTopToBottom, Header:=xlYes
End With

You've mashed together cell addresses with table columns or header labels in hte image so I am not sure if I got the ordinals right. The above will sort with column A as the primary, B as secondary, C as third, D as fourth and E as fifth.

您已经将单元格地址与 hte 图像中的表格列或标题标签混合在一起,所以我不确定我是否正确地获得了序数。以上将按 A 列作为主要,B 列作为次要,C 作为第三列,D 作为第四列和 E 作为第五列进行排序。

回答by 00989021570161

s = ComboBox1.Text
sr = ComboBox1.Text & "4"

Dim xlSort As XlSortOrder
Dim LastRow As Long

With ActiveSheet
  LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
  Sheet9.Range("b4:k5002").Sort Key1:=Sheet9.Range(sr), Order1:=xlAscending, Key2:=Sheet9.Range("e4"), Order2:=xlAscending
End With