【发布时间】: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
【问题讨论】: