首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用多个自定义列表进行排序

使用多个自定义列表进行排序
EN

Stack Overflow用户
提问于 2022-02-02 15:41:42
回答 1查看 136关注 0票数 0

我试图使用两个自定义列表来对特定范围内的列进行排序。

首先定义两个自定义列表(数组),然后在.Sort语句中使列到达,第一列对其进行排序,包括第一个自定义列表,然后是第二列,在第二列上与第二个自定义列表进行排序。

VBA只对第一个自定义列表进行排序,而对第二个自定义列表不做任何操作。

我还没有找到一个使用多个自定义列表和自选择数组的示例。

当我查看Excel时,第二个自定义列表(key2,E >> A)的顺序是不存在的。相反,它采用随机/其他顺序。

如何使用自定义排序列表添加第二级排序?

ordercustom:=Application.CustomListCount + 2没有使用正确的自定义列表。

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2022-02-02 19:01:47

这对我有效-注意排序方法中的列表索引。

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70958519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档