Advertisement
Donnycampo

ApplyColorLogic

Jan 12th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ApplyColorLogic()
  2.     Dim lastRow As Long
  3.     Dim currentRow As Long
  4.  
  5.     ' Find the last row with data in column A
  6.    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
  7.  
  8.     ' Loop through rows starting from 2 to the last row
  9.    For currentRow = 2 To lastRow
  10.         ' Check if C2 has no value
  11.        If IsEmpty(Cells(currentRow, 3).Value) Then
  12.             ' Move to the next row
  13.            GoTo NextRow
  14.         End If
  15.        
  16.         ' If C2 has a value and D2 has a value, turn both C2 and D2 green
  17.        Cells(currentRow, 3).Resize(, 2).Interior.color = RGB(0, 204, 0)
  18.        
  19.         ' If C2 to K2 are all either green or white, turn A2 and B2 green
  20.        If CheckAllGreenOrWhite(currentRow, 3, 11) Then
  21.             Cells(currentRow, 1).Resize(, 2).Interior.color = RGB(0, 204, 0)
  22.         End If
  23.        
  24.         ' If any cells in C2 to K2 are red, turn A2 and B2 red
  25.        If CheckForColor(currentRow, 3, 11, RGB(255, 0, 0)) Then
  26.             Cells(currentRow, 1).Resize(, 2).Interior.color = RGB(255, 0, 0)
  27.         End If
  28.        
  29.         ' If any cells in C2 to K2 are orange (but none are red), turn A2 and B2 orange
  30.        If CheckForColor(currentRow, 3, 11, RGB(255, 192, 0)) And _
  31.            Not CheckForColor(currentRow, 3, 11, RGB(255, 0, 0)) Then
  32.             Cells(currentRow, 1).Resize(, 2).Interior.color = RGB(255, 192, 0)
  33.         End If
  34.        
  35.         ' If any cells in C2 to K2 are yellow (but none are orange or red), turn A2 and B2 yellow
  36.        If CheckForColor(currentRow, 3, 11, RGB(255, 255, 0)) And _
  37.            Not CheckForColor(currentRow, 3, 11, RGB(255, 192, 0)) And _
  38.            Not CheckForColor(currentRow, 3, 11, RGB(255, 0, 0)) Then
  39.             Cells(currentRow, 1).Resize(, 2).Interior.color = RGB(255, 255, 0)
  40.         End If
  41.  
  42. NextRow:
  43.     Next currentRow
  44. End Sub
  45.  
  46. Function CheckAllGreenOrWhite(rowNum As Long, startCol As Long, endCol As Long) As Boolean
  47.     Dim col As Long
  48.     For col = startCol To endCol
  49.         If Cells(rowNum, col).DisplayFormat.Interior.color <> RGB(0, 204, 0) And _
  50.            Cells(rowNum, col).DisplayFormat.Interior.color <> RGB(255, 255, 255) Then
  51.             CheckAllGreenOrWhite = False
  52.             Exit Function
  53.         End If
  54.     Next col
  55.     CheckAllGreenOrWhite = True
  56. End Function
  57.  
  58. Function CheckForColor(rowNum As Long, startCol As Long, endCol As Long, color As Long) As Boolean
  59.     Dim col As Long
  60.     For col = startCol To endCol
  61.         If Cells(rowNum, col).DisplayFormat.Interior.color = color Then
  62.             CheckForColor = True
  63.             Exit Function
  64.         End If
  65.     Next col
  66.     CheckForColor = False
  67. End Function
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement