Code help change cell color...Not working

JaredMcCullough

Well-known Member
Joined
Aug 1, 2011
Messages
516
Its been quite a while since I have used vba/macros. Thought this was an easy one but I guess not. The codes intent is to just change cell color based on 1 of 2 variables. I keep getting an error stating no "for" statement? Wish I stayed more involved so I wouldnt feel so stupid.



Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
 
For Each cell In Range("O5:U5000")
 
    If cell.Value = "P" Then
     cell.Interior.Color = vbGreen
 
    If cell.Value = "F" Then
    cell.Interior.Color = vbRed
 
End If
Next cell
End Sub
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Comfy

Well-known Member
Joined
Dec 21, 2009
Messages
3,377
You're missing an End If

Code:
Dim cell As Range
 
For Each cell In Range("O5:U5000")
 
    If cell.Value = "P" Then
     cell.Interior.Color = vbGreen
 
    If cell.Value = "F" Then
    cell.Interior.Color = vbRed
 
End If
End If
Next cell
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
Office Version
  1. 365
Platform
  1. Windows
Try this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
 
    For Each cell In Range("O5:U5000")
 
        If cell.Value = "P" Then
            cell.Interior.Color = vbGreen
        End If

        If cell.Value = "F" Then
            cell.Interior.Color = vbRed
        End If

    Next cell

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,108,990
Messages
5,526,096
Members
409,685
Latest member
Bellybb

This Week's Hot Topics

Top