gpt4 book ai didi

vba - 按行颜色排序不一致

转载 作者:行者123 更新时间:2023-12-04 21:03:32 25 4
gpt4 key购买 nike

我有一个子程序,它根据在特定列范围内找到的单元格值突出显示每一行,然后它使用下面的代码按特定颜色顺序按单元格颜色对每一行进行排序。 sub 的颜色部分是使用 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

最佳答案

本质上,我是在说“这是您没有问的问题的答案”,因为我不知道 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

关于vba - 按行颜色排序不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com