【问题标题】:Code an Excel VBA sort with a custom order and a value containing commas使用自定义顺序和包含逗号的值对 Excel VBA 排序进行编码
【发布时间】:2011-08-31 08:27:02
【问题描述】:

在 VBA 中,Excel 允许使用 CustomOrder 参数对值进行排序,以选择排序项目的顺序。不幸的是,项目序列由逗号分隔,我的排序项目之一包含逗号。例如,我想按第二列中的类别对第一列中的数据进行排序。 “空中、陆地或海洋”类别包含逗号。

Data1 航空航天
Data2 网络空间
Data3 网络空间
Data4 空中、陆地或海上
Data5 航空航天
Data6 空中、陆地或海上
Data7 网络空间

如果您录制 VBA 宏,则创建的代码如下所示:

MyWorksheet.Sort.SortFields.Add Key:=Range( _
    "B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    CustomOrder:= "Cyberspace,Air,Land,or Sea,Aerospace", _
    DataOption:=xlSortNormal  
MyWorksheet.Sort.Apply

因此,自定义排序顺序应该是“Cyber​​space”,然后是“Air、Land 或 Sea”,然后是“Aerospace”。但是,由于逗号,第二个类别被视为三个类别。带有“Air、Land 或 Sea”的行被排序到底部,因为 Excel 没有为它们找到自定义排序匹配项。 有没有办法让 CustomOrder 使用包含嵌入式逗号的类别?

我尝试在类别周围加上双引号,并尝试用分号替换分隔符逗号(希望 Excel 接受分号而不是逗号)。都没有用。

【问题讨论】:

    标签: sorting vba excel


    【解决方案1】:

    似乎缺少Apply。可以加吗

    MyWorksheet.Sort.Apply
    

    您的自定义订单与我的示例一样。

    编辑根据 OP 更新问题更新

    将宏编辑为以下内容 - 使用 OrderCustom 参数的数组。

    Dim oWorksheet As Worksheet
    Set oWorksheet = ActiveWorkbook.Worksheets("Sheet1")
    Dim oRangeSort As Range
    Dim oRangeKey As Range
    
    ' one range that includes all colums do sort
    Set oRangeSort = oWorksheet.Range("A1:B9")
    ' start of column with keys to sort
    Set oRangeKey = oWorksheet.Range("B1")
    
    ' custom sort order
    Dim sCustomList(1 To 3) As String
    sCustomList(1) = "Cyberspace"
    sCustomList(2) = "Aerospace"
    sCustomList(3) = "Air, Land, or Sea"
    
    Application.AddCustomList ListArray:=sCustomList
    ' use this if you want a list on the spreadsheet to sort by
    ' Application.AddCustomList ListArray:=Range("D1:D3")
    
    oWorksheet.Sort.SortFields.Clear
    oRangeSort.Sort Key1:=oRangeKey, Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    ' clean up
    Application.DeleteCustomList Application.CustomListCount
    Set oWorksheet = Nothing
    

    【讨论】:

    • 我原来的例子不是很清楚。请阅读更新后的问题。
    • @Dean Hill 更新了我的答案。
    • 我遵循了这个可行的解决方案 - 除了它在运行宏后尝试保存时导致 Excel 2013 崩溃。解决方法是添加一行:ActiveSheet.Sort.Sortfields.Clear 行前:Application.DeleteCustomList Application.CustomListCount 详情在这里:answers.microsoft.com link
    • @Slab,谢谢你,我的 excel 在应用你的解决方案后仍然崩溃,还有其他解决方法吗?
    • @excelguy 抱歉,我不确定。也许对我的微软链接的最后评论可能会有所帮助? “您必须清除使用它的每个工作表上的排序字段”
    【解决方案2】:

    好的...根据更新的描述,您要排序的列旁边的公式怎么样。

    因此,如果“空中、陆地或海洋”在 B1 列中,则 C1 将具有:

    =SUBSTITUTE(B1,",","|")
    

    然后您可以像这样进行自定义排序:

    MyWorksheet.Sort.SortFields.Add Key:=Range( _
    
            "B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            CustomOrder:= "Cyberspace,Air|Land|or Sea,Aerospace", _
            DataOption:=xlSortNormal  
        MyWorksheet.Sort.Apply
    

    确保适当调整范围。

    【讨论】:

    • 我原来的例子不是很清楚。请阅读更新后的问题。很抱歉造成混乱。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2014-04-19
    相关资源
    最近更新 更多