VBA code to colour dependent on cell values

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
582
Office Version
  1. 365
Is it possible to colour cells dependent on values?

On even rows (starting row 2) - Columns C:F - If "Yes" 169,208,12. If "No" 198,224,180
On odd rows (starting row 3) - Columns C:F -If "Yes" 155, 194, 230. If "No" 189, 215, 238

Finishing row 479
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
try this.

Code:
Sub Color_it()

For r = 2 To 6 Step 2
Set myRange = Range("C" & r & ":F" & r)
If Cells(r, "A") = "Yes" Then myRange.Interior.Color = RGB(169, 208, 12) Else myRange.Interior.Color = RGB(198, 224, 180)
Set myRange = Range("C" & r + 1 & ":F" & r + 1)
If Cells(r + 1, "A") = "Yes" Then myRange.Interior.Color = RGB(155, 194, 230) Else myRange.Interior.Color = RGB(189, 215, 238)
Next r

End Sub
 
Upvote 0
Hi surkdidat,

Try the below code

Code:
Sub ColorCells()
Dim Rg As Range, cell As Range
Set Rg = ActiveSheet.Range("C2:F479")
For Each cell In Rg
    If cell.Row Mod 2 = 1 Then
        If cell.Value = "Yes" Then cell.Interior.Color = RGB(155, 194, 230)
        If cell.Value = "No" Then cell.Interior.Color = RGB(189, 215, 238)
    Else
        If cell.Value = "Yes" Then cell.Interior.Color = RGB(169, 208, 12)
        If cell.Value = "No" Then cell.Interior.Color = RGB(198, 224, 180)
    End If
Next
End Sub

Edit : rpaulson posted a reply while I was writing my code :)
 
Last edited:
Upvote 0
Another option
Code:
Sub ColourRows()
   Dim i As Long
   For i = 2 To Range("C" & Rows.count).End(xlUp).Row Step 2
      Range("C" & i).Resize(, 4).Interior.Color = IIf(Range("C" & i).Value = "Yes", RGB(169, 208, 12), RGB(198, 224, 180))
      Range("C" & i + 1).Resize(, 4).Interior.Color = IIf(Range("C" & i + 1).Value = "Yes", RGB(155, 194, 230), RGB(189, 215, 238))
   Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,835
Members
449,343
Latest member
DEWS2031

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top