2014-05-15 63 views
0

我已經有條件地在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 

回答

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 
'... 
+0

儘管這減少了重複硬編碼它確實有一個較長的運行時間是一種糟糕的用戶體驗。有什麼建議麼? – bhinton

+0

我想我需要更多關於該項目及其目標的信息。 56種非常多!有56種不同的顏色?你有一個方便的例子嗎? –

+0

我想在幾個不同的項目中使用它,其中一些可能只使用3種顏色,而另一些則使用全部56種顏色。 – bhinton