【问题标题】:Custom sorting with custom ordering自定义排序与自定义排序
【发布时间】:2015-09-20 16:56:50
【问题描述】:

我想根据G 列中的值对我的行进行排序。有 3 个可能的值:GreenRedYellow。我希望行以 Green 排序,然后是 Yellow,然后是 Red

我尝试的所有结果都是按字母顺序排列的:绿色红色然后是黄色R 列有二次排序,但工作正常。

我的最新代码如下。 rr 是最后一行的编号。我已经尝试过使用和不使用Order1:=xlAscending

sCustomList = "绿色" "黄色" "红色"

Application.AddCustomList ListArray:=sCustomList
    Range("A3:T" & rr).Sort Key1:=Range("G3:G" & rr), Order1:=xlAscending, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    DataOption1:=xlSortNormal, Key2:=Range("R3:R" & rr), Order2:=xlAscending

【问题讨论】:

  • 谢谢吉普德。我知道它是如何工作的,但我需要能够使用 VBA 来做到这一点。这是一个更大的宏的一部分

标签: vba excel sorting


【解决方案1】:

查看您的代码,sCustomList 看起来像一个字符串类型变量,而不是一个变体数组。我在自定义排序列表方面的成功在于每次都创建一个新的并使用最高的索引号来引用它。

Sub custom_sort()
    Dim vCustom_Sort As Variant, rr As Long

    vCustom_Sort = Array("green", "yellow", "red", Chr(42))
    Application.AddCustomList ListArray:=vCustom_Sort

    With Worksheets("Sheet2")    '<~~ set this properly!
        .Sort.SortFields.Clear
        rr = .Cells(Rows.Count, "G").End(xlUp).Row
        With .Range("A3:T" & rr)
            'use this to presort one or more secondary fields before the primary custom sort
            '.Cells.Sort Key1:=.Columns(18), Order1:=xlAscending, _
                        Key2:=.Columns(1), Order2:=xlDescending, _
                        Orientation:=xlTopToBottom, Header:=xlYes
            .Cells.Sort Key1:=.Columns(7), Order1:=xlAscending, _
                        Orientation:=xlTopToBottom, Header:=xlYes, MatchCase:=False, _
                        OrderCustom:=Application.CustomListCount + 1

        End With
        .Sort.SortFields.Clear
    End With

End Sub

.Cells.Sort.SortFields.Add.Cells.Sort 之间存在转折,通常会产生一些混淆。 .SortFields.Add method 使用 CustomOrder:= 参数,Range.Sort method 使用 OrderCustom:= 参数。两者绝对不一样,但经常交替使用,结果是灾难性的。

【讨论】:

  • 对于自定义列表,我使用 GetCustomListNum 方法查看列表是否存在。如果没有,该方法返回0
【解决方案2】:

如果您使用 SortFields 对象,则不必引用自定义列表: 更改各种范围参考的位置应该很明显。我还在G以外的一列上添加了字母排序


Option Explicit
Sub TrafficLightSort()
    Dim WS As Worksheet
    Dim rSortRange As Range, rSortKey As Range
    Const sSortOrder As String = "Green,Yellow,Red"

Set WS = Worksheets("sheet1")
With WS
    Set rSortRange = Range("E1", .Cells(.Rows.Count, "E").End(xlUp)).Resize(columnsize:=3)
    Set rSortKey = rSortRange.Columns(3)

    With .Sort.SortFields
        .Clear
        .Add Key:=rSortKey, _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            CustomOrder:=sSortOrder
        .Add Key:=rSortRange.Columns(1), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending
    End With

    With .Sort
        .SetRange rSortRange
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With
End With

End Sub

【讨论】:

    【解决方案3】:

    我建议在您的工作表中添加一个具有明确排序顺序的表格(绿色 = 1、黄色 = 2 等)。然后将一列添加到您的排序范围,该列使用查找函数返回排序值,并使用 VBA 基于该列运行标准排序。这将为您提供更易于开发人员阅读的 VBA 代码,允许不阅读 VBA 的 Excel 用户查看您的排序顺序,并避免将硬编码值深埋在宏中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2014-07-18
      • 2011-12-16
      • 2012-01-01
      • 2011-01-11
      • 2011-03-08
      • 2014-04-29
      相关资源
      最近更新 更多