【发布时间】:2010-11-29 20:45:34
【问题描述】:
我在 Excel 2003 中使用 VBA 对列进行排序。我需要按第 5 列升序排序,然后使用自定义顺序对第 3 列进行排序,然后按第 4 列升序排序。我很难让排序工作,我不完全理解 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
【问题讨论】: