基于颜色对行进行排序

时间:2014-05-15 20:37:58

标签: sorting excel-vba colors vba excel

我已在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

1 个答案:

答案 0 :(得分:0)

假设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
'...