2015-06-17 52 views
2

我有一个基于在特定列范围内找到的单元格值突出显示每行的子,然后它使用下面的代码按特定颜色顺序按其单元格颜色对每行进行排序。子的颜色部分是使用FindAllOccurrences函数完成的,如果在此问题上找到公式Sorting and color-coding based on variables。我遇到的问题是使用排序功能。目前的形式,它的工作原理,但不是绝对的。我的意思是,最后一个颜色是红色,仅用于突出显示我的工作表上已停用的项目,排序函数应该对最后一个进行排序,如果它存在,它将执行排序,如果它不存在,它似乎覆盖无论是否停用,我的纸张上的最后一种颜色都会变成红色。我通过改变不同的变量来测试它,并得到不同的结果。按行颜色排序不一致

它可能是我没有看到的东西,但唯一似乎适合它为什么不一致的是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

这是一个有趣的想法,我不知道这是可能的。你能回答更多细节吗?用这种方法你可能想要指定一个优先顺序吗?这不是绝对必要的,但如果可能的话,将是理想的。 – proxy156

+0

我认为它可能会太复杂,试图检索RGB - 对不起。删除了我的评论。 – Raystafarian

回答

0

本质上讲,我说:“这里有一个答案,你也没问一个问题:”因为我不知道为什么 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 
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 

末次