我有一个sub,根据在特定列范围内找到的单元格值突出显示每一行,然后使用下面的代码按照特定颜色顺序按单元格颜色对每一行进行排序。使用FindAllOccurrences函数完成sub的颜色部分,如果在此问题上找到公式Sorting and color-coding based on variables。我遇到的问题是sort函数。目前的形式,它有效,但不是绝对的。我的意思是,最后一种颜色是红色,仅用于突出我工作表上已停产的项目,排序功能应该排序最后,如果它在那里它会做,如果它不存在,它似乎覆盖无论是否已停止,我的工作表上的最后一种颜色都会变成红色。我通过改变不同的变量来测试它,我得到了不同的结果。
这可能是我没有看到的东西,但似乎唯一适合它为什么不一致的是sort函数不包含if命令。如果没有找到颜色,代码中的任何内容都不允许它跳过。我认为,因为它是一个排序功能,它无法排序不存在的颜色,所以它会自动跳到找到的下一个颜色,但也许我不正确?有什么建议?
Set WS = ThisWorkbook.Sheets(1)
Set LastRow = WS.Range("FU3:FU5002")
With WS.Sort
.SortFields.Clear
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(204, 255, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(102, 0, 102)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(204, 255, 204)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(128, 128, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(150, 150, 150)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 255, 204)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(204, 204, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(0, 128, 128)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(102, 102, 153)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(192, 192, 192)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(128, 128, 128)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(0, 255, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(0, 255, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(128, 0, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 255, 153)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(128, 0, 128)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(0, 102, 204)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(153, 204, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 153, 204)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(204, 153, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 0, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 153, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 0, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(153, 51, 102)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(153, 204, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 204, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(0, 204, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(0, 0, 255)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 102, 0)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(51, 153, 102)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(51, 51, 153)
.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = RGB(255, 0, 0)
.SetRange DataSheet
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
答案 0 :(得分:0)
基本上,我说'#34;这里是对你没有问过的问题的回答"因为我不知道为什么 excel正在这样做,但是因为我在评论中提出它,我会发布它。我无法想象这是一种良好的做法。
将独特颜色读入字典并将其转换为RGB数组
然后我想为每个sortfield
添加过滤器 - 但这可能是荒谬的,因为我想你有一个你想要的配色方案。因此,可以采用颜色的顺序并比较每个颜色以查看它是否在数组中,如果是,则进行排序。否则,跳过颜色?
Sub test()
Dim col As Variant
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
For Each c In Range("FU3:FU5002")
col = (c.Interior.Color)
d(col) = 1
Next
Dim v As Variant
Dim j As Integer
j = d.Count
Dim colores() As String
ReDim colores(j)
i = 1
For Each v In d.keys
colores(i) = Convert_Dec2RGB(v)
i = i + 1
Next
'With ActiveSheet.Sort
'For k = 1 To j
'.SortFields.Add(LastRow, xlSortOnCellColor, xlAscending, , xlSortNormal). _
SortOnValue.Color = colores(k)
'Next
'End With
End Sub
Private Function Convert_Dec2RGB(ByVal myDECIMAL As Long) As String
'https://socko.wordpress.com/2008/10/07/convert-decimal-to-rgb-color-value/
Dim myRED As Long
Dim myGREEN As Long
Dim myBLUE As Long
myRED = myDECIMAL And &HFF
myGREEN = (myDECIMAL And &HFF00&) \ 256
myBLUE = myDECIMAL \ 65536
Convert_Dec2RGB = "RGB(" & CStr(myRED) & "," & CStr(myGREEN) & "," & CStr(myBLUE) & ")"
End Function
答案 1 :(得分:0)
尝试使用以下代码。 你必须找到你想要排序的所有颜色代码,并为你想要的顺序添加颜色代码。
子测试()
Dim dis As Object
Set dis = CreateObject("Scripting.Dictionary")
P = 1
Rcnt = Range("AU65000").End(xlUp).Row
For i = 3 To Rcnt
On Error Resume Next
dis.Add Range("A" & i).Interior.Color, P
P = P + 1
Next i
'sorting
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
'red
If dis.exists(255) = True Then ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add(Range("A2:A10"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = 255
'yellow
If dis.exists(65535) = True Then ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add(Range("A2:A10"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = 65535
'blue
If dis.exists(12874308) = True Then ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add(Range("A2:A10"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = 12874308
'orange
If dis.exists(3243501) = True Then ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add(Range("A2:A10"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = 3243501
'green
If dis.exists(5287936) = True Then ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add(Range("A2:A10"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = 5287936
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("A2:A10")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub