• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Elseif vba code not working right - Solved

Dokat

Member
Hi,
I have below simple VBA code where if the value in CE1 is 1 then highlight cell D5 in White otherwise in Red. When i run the code it highlights in red when the value is other then 1 but it doesnt highlight it when CE1 is equal to 1. Can someone please help? Thanks

Code:
Sub ColorD5()
   Dim r1 As Range, r2 As Range
      Set r1 = Range("CE1")
      Set r2 = Range("D5")
      If r1.Value = 1 Then
      r2.Interior.Color = vbWhite
      Else
      r2.Interior.Color = vbRed
End If
End Sub

Update - Below did the trick


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
   
    Set KeyCells = Range("CE1")
      Dim r1 As Range, r2 As Range
      Set r1 = Range("CE1")
      Set r2 = Range("D5")
      If r1.Value = 1 Then
      r2.Interior.Color = vbWhite
      Else
      r2.Interior.Color = vbRed
End If
End Sub
 
Last edited:
Back
Top