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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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,216,360
Messages
6,130,175
Members
449,562
Latest member
mthrasher16

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