Home > front end >  Color rows depending on another columns by numbers
Color rows depending on another columns by numbers

Time:11-07

I am trying to modify the below code. Sheet from 1 to 8, maybe less. Sheet 1 = Column A contain number, Column B contain numbers that group column A. Column A Column B 11200 3 11202 3 12500 4 12502 4 And more rows down, so even number in column B=color blue, odd number columnB= color green Ineed to apply more colors, so I need 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

enter image description here

Please check the picture above in this post, Column A need only the colors, depending on when Column B has even, odd, even 1, odd 1.

CodePudding user response:

Use 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
  • Related