用VBA多列sorting

我正在使用VBA对Excel 2003中的列进行sorting。我需要按第5列升序sorting,然后第3列使用自定义顺序,然后按第4列升序sorting。 我很难得到这样的工作,我不完全理解OrderCustom如何适用。

任何指针在正确的方向将不胜感激:)我的代码如下。

With wsData lastrow = .Cells(Rows.Count, 1).End(xlUp).Row + 1 lastCol = .Cells(4, Columns.Count).End(xlToLeft).Column Dim n As Long Application.AddCustomList ListArray:=Array("LOW", "MEDIUM OR HIGH", "HIGH ONLY") n = Application.GetCustomListNum(Array("LOW", "MEDIUM OR HIGH", "HIGH ONLY")) + 1 Dim strSortOrder As String .Range(.Cells(1, 1), .Cells(lastrow, lastCol)).Sort _ Key1:=.Range(.Cells(2, 5), .Cells(lastrow, lastCol)), Order1:=xlAscending, _ Key2:=.Range(.Cells(2, 3), .Cells(lastrow, lastCol)), Order2:=xlDescending, _ Key3:=.Range(.Cells(2, 4), .Cells(lastrow, lastCol)), Order3:=xlDescending, _ OrderCustom:=n, _ MatchCase:=False, Orientation:=xlSortColumns, Header:=xlYes End With 

尝试将您的sorting分成3个独立的步骤,只有第二个使用您的自定义sorting顺序,即

 .Range(.Cells(1, 1), .Cells(lastrow, lastCol)).Sort _ Key1:=.Range(.Cells(2, 4), .Cells(lastrow, lastCol)), Order1:=xlDescending, _ MatchCase:=False, Orientation:=xlSortColumns, Header:=xlYes .Range(.Cells(1, 1), .Cells(lastrow, lastCol)).Sort _ Key1:=.Range(.Cells(2, 3), .Cells(lastrow, lastCol)), Order1:=xlDescending, _ OrderCustom:=n, _ MatchCase:=False, Orientation:=xlSortColumns, Header:=xlYes .Range(.Cells(1, 1), .Cells(lastrow, lastCol)).Sort _ Key1:=.Range(.Cells(2, 5), .Cells(lastrow, lastCol)), Order1:=xlAscending, _ MatchCase:=False, Orientation:=xlSortColumns, Header:=xlYes 

请注意,我将这些sorting的执行顺序与原始声明中声明的顺序进行了比较。