excel VBA根据递增的字段值突出显示行的“集合”

1cosmwyk  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(79)

我有一长串代码,成功地创建了一个大的数据集。
最后一项任务(CODE NOT WRITTEN)是用相同的颜色突出显示所有填充的列(A:BD),并且每次标识符(列B中)更改时,该颜色都会更改。每个ID可以有1-10行与其关联。
我需要的颜色是粉彩(所以你仍然可以阅读黑色文字)。
我很好地在代码中硬编码了一组RGB(可能是10种颜色),只是让高亮部分连续循环这些颜色。
我想用psuedo表达的意思是

c1 = RGB(R, G, B)
c2 = RGB(R, G, B)
c3 = RGB(R, G, B)
c4 = RGB(R, G, B)
c5 = RGB(R, G, B)

Sheet1.Range("A1:BD1").Interior.Color = c1

if b2=b1 Sheet1.Range("A2:BD2").Interior.Color = c1
   else Sheet1.Range("A2:BD2").Interior.Color = c2

if b3=b2 Sheet1.Range("A2:BD2").Interior.Color = c2
   else Sheet1.Range("A2:BD2").Interior.Color = c3

字符串

nx7onnlm

nx7onnlm1#

试试这样的方法:

Sub ColorRows()
    Dim rngColors As Range, rw As Range, i As Long, ws As Worksheet
    Dim rngToColor As Range, numColors As Long, clr As Long
    
    'setting the background color of each cell in this range...
    Set rngColors = ThisWorkbook.Worksheets("setup").Range("A1:A10") 'for example
    numColors = rngColors.Cells.Count 'how many colors
    
    Set ws = ActiveSheet 'or some specific worksheet
    Set rngToColor = ws.Range("A2:BD" & ws.Cells(Rows.Count, "B").End(xlUp).row)
    
    i = 1
    clr = rngColors(i).Interior.Color 'starting color
    For Each rw In rngToColor.Rows
        rw.Interior.Color = clr
        'value in B in next row is different?
        If rw.Cells(2).Value <> rw.Cells(2).Offset(1).Value Then
            i = i + 1
            If i > numColors Then i = 1 'loop back to the start?
            clr = rngColors(i).Interior.Color 'next color
        End If
    Next rw
End Sub

字符串

相关问题