change color of a cell, depending on value

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
339
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet where the color of cells A3:A5 should change, depending on the value. I don't want to work with conditional formatting, so I wrote the VBA code below. However, all cells stay in RED, no matter which value.

What is wrong with my VBA ? See also the image.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim col_A_rng As Range
Set col_A_rng = Range("A3:A5")

If Target.Count = 1 And Not Application.Intersect(Target, col_A_rng) Is Nothing Then
    If Target.Value = 0 Then Target.Interior.Color = RGB(255, 0, 0)
    ElseIf Target.Value = 1 Then Target.Interior.Color = RGB(0, 255, 0)
    Else
End If

End Sub
 

Attachments

  • red_or_green.png
    red_or_green.png
    13.2 KB · Views: 11

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try as below, i have swapped the IF statements for a select case:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim col_A_rng As Range
    
    Set col_A_rng = Range("A3:A5")
    
    If Target.Count = 1 And Not Intersect(Target, col_A_rng) Is Nothing Then
        Select Case Target.Value
            Case 0
                Target.Interior.Color = vbRed
            Case 1
                Target.Interior.Color = vbGreen
        End Select
    End If
End Sub
 
Upvote 0
Solution
Thanks, your solution works fine !

I am still wondering what can be wrong with my IF statements...
 
Upvote 0
I don't think the 'Else' were needed, i think the Ifs would have worked as below:
VBA Code:
If Target.Count = 1 And Not Application.Intersect(Target, col_A_rng) Is Nothing Then
    If Target.Value = 0 Then Target.Interior.Color = RGB(255, 0, 0)
    If Target.Value = 1 Then Target.Interior.Color = RGB(0, 255, 0)
End If

Sometimes i think select case is easier to read and add to in some instances.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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