我试图使用两个自定义列表来对特定范围内的列进行排序。
首先定义两个自定义列表(数组),然后在.Sort
语句中使列到达,第一列对其进行排序,包括第一个自定义列表,然后是第二列,在第二列上与第二个自定义列表进行排序。
VBA只对第一个自定义列表进行排序,而对第二个自定义列表不做任何操作。
我还没有找到一个使用多个自定义列表和自选择数组的示例。
当我查看Excel时,第二个自定义列表(key2,E >> A)的顺序是不存在的。相反,它采用随机/其他顺序。
如何使用自定义排序列表添加第二级排序?
ordercustom:=Application.CustomListCount + 2
没有使用正确的自定义列表。
Sub MultipleCustomLists()
Dim vSortLIst As Variant
Dim vSortLIst2 As Variant
vSortLIst2 = Array("E", "D", "C", "B", "A")
vSortLIst = Array("1", "2", "3", "4", "5")
Application.AddCustomList ListArray:=vSortLIst2
Application.AddCustomList ListArray:=vSortLIst
Columns("A:C").Sort Key1:=[A:A], ordercustom:=Application.CustomListCount + 1, _
key2:=[B:B], ordercustom:=Application.CustomListCount + 2, _
Orientation:=xlTopToBottom, Header:=xlYes
ActiveSheet.Sort.SortFields.Clear
Application.DeleteCustomList Application.CustomListCount
Application.DeleteCustomList Application.CustomListCount
End Sub
发布于 2022-02-02 19:01:47
这对我有效-注意排序方法中的列表索引。
Sub MultipleCustomLists()
With Application
Debug.Print "before add", .CustomListCount
.AddCustomList ListArray:=Array("E", "D", "C", "B", "A")
.AddCustomList ListArray:=Array("1", "2", "3", "4", "5")
Debug.Print "after add", .CustomListCount
End With
DoEvents
ActiveSheet.Range("A:C").Sort _
Key1:=[A2], ordercustom:=Application.CustomListCount, _
key2:=[B2], ordercustom:=Application.CustomListCount + 1, _
Orientation:=xlTopToBottom, Header:=xlYes
ActiveSheet.Sort.SortFields.Clear
With Application
.DeleteCustomList .CustomListCount
.DeleteCustomList .CustomListCount
Debug.Print "after delete", .CustomListCount
End With
End Sub
https://stackoverflow.com/questions/70958519
复制相似问题