|
本帖最后由 一念天堂 于 2018-5-27 22:00 编辑
- Sub Macro1()
- Application.ScreenUpdating = False
- With Range("A3:R" & Range("A65536").End(xlUp).Row)
- .Sort Key1:=Range("F3"), Order1:=1, key2:=Range("C3"), order2:=1, key3:=Range("d3"), order3:=1, key4:=Range("e3"), order4:=1, Header:=xlNo
- Application.AddCustomList ListArray:=Array("甲1", "甲2", "甲3", "甲4", "甲5", "甲6", "甲7", "甲8", "甲9")
- .Sort Key1:=Range("b3"), Order1:=1, Header:=xlNo, OrderCustom:=Application.CustomListCount + 1
- Application.DeleteCustomList ListNum:=Application.CustomListCount
- Application.AddCustomList ListArray:=Array("一队", "二队", "三队")
- .Sort Key1:=Range("a3"), Order1:=1, Header:=xlNo, OrderCustom:=Application.CustomListCount + 1
- Application.DeleteCustomList ListNum:=Application.CustomListCount
- End With
- Application.ScreenUpdating = True
- End Sub
如上图,如果D列和E列也有内容并且也需要自动排序的话,添加红色部分:key3:=Range("d3"), order3:=1, 可以运行,但是继续添加key4:=Range("e3"), order4:=1,就提示未找到命名参数;
后来看了下,原来最多只能对3列排序,如果超出3列的话,只能分组写吗?分成多组排序?
另外,VBA宏多个列同时排序时,要和工作表里排序列的顺序颠倒吗?比如工作表排序的列顺序是A,B,C,D;VBA里写的时候要把D列放到KEY1吗?
|
|