【问题标题】:Sorting Rows based on Color根据颜色对行进行排序
【发布时间】:2014-07-04 11:41:00
【问题描述】:

我在 Excel 中对工作表进行了有条件的格式化,这样如果单元格包含相同的文本值,它们就会被标记为相同的颜色。我想根据颜色自动对行进行排序。有没有一种方法可以自动对行进行排序,而无需通过硬编码和定义要排序的行的顺序?

这是我目前所做的:有没有办法让这变得更有活力?

   Rows("7:7").Select 'Indicates Header Row
   Range("B7").Activate 'Indicates Which Column to Sort Rows Based On
   Selection.AutoFilter
   ActiveSheet.AutoFilter.Sort.SortFields.Clear
   ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 1
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
       ActiveSheet.AutoFilter.Sort.SortFields.Clear
   ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 2
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
          ActiveSheet.AutoFilter.Sort.SortFields.Clear
   ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 3
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 4
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 5
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 6
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 7
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 8
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 9
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 10
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 11
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 12
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 13
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 14
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 15
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 16
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 17
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 18
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 19
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 20
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 21
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 22
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 23
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 24
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 25
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 26
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 27
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 28
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 29
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 30
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 31
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 32
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 33
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 34
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 35
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 36
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 37
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 38
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 39
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 41
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 42
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 43
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 44
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 45
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 46
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 47
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 48
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 49
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 50
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 51
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 52
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 53
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 54
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 55
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add(Range("B7") _
    , xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.ColorIndex = 56
With ActiveSheet.AutoFilter.Sort
    .header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub

【问题讨论】:

    标签: sorting excel colors vba


    【解决方案1】:

    假设 SortAutofilter 在此处符合您的规范,看起来您可以使用 For 循环来节省一些主要重复:

    Dim Index As Long
    Dim MySheet As Worksheet
    
    'set reference up-front
    Set MySheet = ThisWorkbook.ActiveSheet
    
    '...
    'do whatever you need to for set up
    '...
    
    'set the 56 term however you see fit... find the last row maybe?
    For Index = 1 To 56
        With MySheet.Sort.SortFields
            .Clear
            .Add(Range("B7"), xlSortOnCellColor, xlAscending, , _
                xlSortNormal).SortOnValue.ColorIndex = Index '<~ loop comes in handy
        End With
        With MySheet.AutoFilter.Sort
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next Index
    '...
    'do whatever you need to clean up and finish
    '...
    

    【讨论】:

    • 虽然这减少了重复的硬编码,但它确实有更长的运行时间是糟糕的用户体验。有什么建议吗?
    • 我想我需要更多关于该项目及其目标的信息。 56种是相当多的!有56种不同的颜色吗?你有一个方便的例子吗?
    • 我想在几个不同的项目中使用它,其中一些可能只使用 3 种颜色,而另一些则使用全部 56 种颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 2016-11-13
    • 2013-12-11
    • 1970-01-01
    • 2018-03-16
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多