根据数字的另一列为行着色

Color rows depending on another columns by numbers

我正在尝试修改以下代码。 Sheet 从 1 到 8,也许更少。 Sheet 1 = A 列包含数字,B 列包含对 A 列进行分组的数字。 A 列 B 列 11200 3 11202 3 12500 4 12502 4 向下更多行,所以 B 列中的偶数 = 蓝色,奇数 B 列 = 绿色 我需要应用更多颜色,所以我需要 iseven+1=color yellow, isodd+1=color brown.

    Sub Color()

Dim CvbRed, cYellow, cGreen, cBlue As Integer

For Each cell In Range("B5:B" & Range("A" & Rows.Count).End(xlUp).Row)

 Select Case Color
 
Case IsEven
Range("A5:A").Cells.Interior.Color = vbRed
        cRed = cRed + 1

Case IsOdd
Range("A5:A").Cells.Interior.Color = vbYellow
        cYellow = cYellow + 1
  
Case IsEven + 2
Range("A5:A").Cells.Interior.Color = vbGreen
        cGreen = cGreen + 1
    
Case IsOdd + 2
Range("A5:A").Cells.Interior.Color = vbBlue
        cBlue = cBlue + 1
    
    End Select
Next cell
End Sub

这个post请看上图,A列只需要颜色,看B列什么时候有偶数,奇数,偶数+1,奇数+1。

使用Mod

Option Explicit
Sub ColorMacro()

    Dim wb As Workbook, cell As Range, lastrow As Long
    Dim n As Integer, i As Integer
    Dim arColor ' odd-green, even-blue, odd+1-brown, even+1-yellow
    arColor = Array(RGB(128, 255, 128), _
              RGB(128, 128, 255), _
              RGB(200, 150, 100), _
              RGB(255, 255, 128))
     
    Set wb = ThisWorkbook
    For n = 2 To wb.Sheets.Count
        With wb.Sheets(n)
            lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
            For Each cell In .Range("B2:B" & lastrow)
                i = (cell.Value - 1) Mod 4
                cell.Offset(,-1).Interior.Color = arColor(i)
            Next
        End With
    Next
    
End Sub