我正在尝试使用3个以上的键对工作表进行排序。
我了解到我需要拆分密钥,但是找不到适合我的任何东西。
我想添加此VBA,以便它可以在任何工作簿/工作表中使用。
Sub Sort()
With Range("A1", Range("AZ" & Rows.Count).End(xlUp))
.Sort Key1:=.Cells(1, 32), Order1:=xlDescending, Header:=xlYes, _
Key2:=.Cells(1, 31), Order1:=xlDescending, Header:=xlYes, _
Key3:=.Cells(1, 26), Order1:=xlDescending, Header:=xlYes, _
Key4:=.Cells(1, 25), Order1:=xlDescending, Header:=xlYes, _
Key5:=.Cells(1, 15), Order1:=xlDescending, Header:=xlYes, _
Key6:=.Cells(1, 5), Order1:=xlDescending, Header:=xlYes, _
Key7:=.Cells(1, 4), Order1:=xlDescending, Header:=xlYes
End With
End Sub
编辑:最终的VBA代码
Sub Sort()
Range("A1", Range("AZ" & Rows.Count).End(xlUp)).Select
Selection.Sort Key1:=Range("D1"), _
Order1:=xlDescending, Header:=xlYes, _
Key2:=Range("E2"), _
Order2:=xlDescending, Header:=xlYes, _
Key3:=Range("O2"), _
Order3:=xlDescending, Header:=xlYes
Range("A1", Range("AZ" & Rows.Count).End(xlUp)).Select
Selection.Sort Key1:=Range("Y2"), _
Order1:=xlDescending, Header:=xlYes, _
Key2:=Range("Z2"), _
Order2:=xlDescending, Header:=xlYes, _
Key3:=Range("AE2"), _
Order3:=xlDescending, Header:=xlYes
Range("A1", Range("AZ" & Rows.Count).End(xlUp)).Select
Selection.Sort Key1:=Range("AF2"), _
Order1:=xlDescending, Header:=xlYes
End Sub
答案 0 :(得分:1)
以前我遇到过同样的问题,我必须对8列进行排序,这是通过以下方式解决的:
(基本上,您需要从头开始一次又一次地对选择进行重新排序)
'custom sort b d g j m p s v
Range("A4:X757").Select
Selection.Sort Key1:=Range("V4"), _
Order1:=xlAscending, _
Key2:=Range("S4"), _
Order2:=xlAscending, _
Key3:=Range("P4"), _
Order3:=xlAscending
Range("A4:X757").Select
Selection.Sort Key1:=Range("M4"), _
Order1:=xlAscending, _
Key2:=Range("J4"), _
Order2:=xlAscending, _
Key3:=Range("G4"), _
Order3:=xlAscending
Range("A4:X757").Select
Selection.Sort Key1:=Range("B4"), _
Order1:=xlAscending, _
Key2:=Range("D4"), _
Order2:=xlAscending