按行颜色sorting不一致

我有一个基于在特定的列范围内find的单元格值来突出显示每行的子,然后使用下面的代码按照特定的颜色顺序通过其单元格颜色对每行进行sorting。 子的颜色部分是使用FindAllOccurrences函数完成的,如果在这个问题上find公式,则根据variables进行sorting和颜色编码 。 我遇到的问题是与sortingfunction。 在目前的forms,它的作品,但不是绝对的。 我的意思是,最后一个颜色是红色的,只是用来突出显示我的工作表上已经停止使用的项目,sorting函数应该sorting最后一个,如果它存在的话,它会这样做,如果它不存在,无论是否停用,我的纸张上最后一种颜色的颜色。 我已经通过改变不同的variables来testing它,我得到了不同的结果。

这可能是我没有看到的东西,但唯一似乎适合它为什么不一致的是sort函数不包含if命令。 如果没有find颜色,则代码中没有任何内容允许跳过。 我认为,因为它是一种sortingfunction,它不能sorting不在那里的颜色,所以它会自动跳到find的下一个颜色,但也许我是不正确的? 有什么build议么?

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 

从本质上讲,我的意思是“这里是对你没有问的问题的回答”,因为我不知道为什么 excel是这样做的,但是因为我在评论中提到了它,所以我会把它发布出来。 我不认为这是做这件事的方法。

将独特的颜色读入字典并将其转换为RGB数组

然后我想为每个sortfield添加filter – 但是这将是荒谬的,因为我想你有一个你想要的配色scheme。 所以,也许可以采取你的颜色的顺序,并比较每个,看看它是否在数组中,如果是的话,那么sorting。 否则,跳过颜色?

 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 

尝试使用下面的代码。 你必须find你想要sorting的所有颜色代码,并添加你想要的顺序的颜色代码。

小测()

 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 

结束小组