Excel VBA – 多级sorting

如何更改下面的代码以多层次的方式进行sorting? 目前,代码一次一列地对表格进行sorting,我想把它们排列在一起进行多级sorting。

以下是我试图实现的:

在这里输入图像说明

这里是我的代码,它一次一列地sorting表:

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 

我如何改变上面的所有内容?

我总是build议摆脱录制的.Sort方法,以'只有你需要'的VBAsorting代码。 但是,存在一个问题,即每个sorting最多只能sorting三个sorting键; 解决scheme是执行两个sorting操作。 sorting最高的序数,然后最后三个主要sorting序号。

 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 

你已经把表格列或者标题标签的单元格地址拼凑在一起,所以我不确定是否正确。 以上将以A列为主,B为次要,C为第三,D为第四,E为第五。