VBA change shape color based on cell value

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
Hi
I am using a code so that the shape changes the color when a cell value is changed.
My problem is that cell J24 is changing as a result of a formula recalculating. So what I have to change in my code shown below to make it work?
I prefer that the code will work for 2 shapes since I need to add another shape.

Thanks in advance

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("J24")) Is Nothing Then Exit Sub
If IsNumeric(Target.Value) Then
If Target.Value < 80000 Then
ActiveSheet.Shapes("LevelA").Fill.ForeColor.RGB = vbRed
ElseIf Target.Value >= 80000 And Target.Value < 400000 Then
ActiveSheet.Shapes("LevelA").Fill.ForeColor.RGB = vbYellow
Else
ActiveSheet.Shapes("LevelA").Fill.ForeColor.RGB = vbGreen
End If
End If
End Sub
 
Hi,
Sorry at this moment I can't do this since I'm at work but tomorrow I will give you the link and details.
Till now thank you for your help.
Happy Feasts
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi mumps,
The file has my original code since when I did yours, the tanks where not being updated.

Details:


This file is just for testing, so please ignore things that are not in place and I replaced some figures so that you can work without problems.
Tank A
This must be equal to cell J24. To bring the tank less than 80000 you must write figures in range A15 to A22 which at least bring a total of 320001 in cell A23 and to bring the tank more than 80000 you need at least a total of 320000 or less.


Tank B
This must be equal to cell H24 which appears when you write figures in cell G24. Now since this is just for testing you can write only figures from 4000 to 4010 to get the tank less than 80000 and 4020 to 4120 to get the tank from 80000 to 400000.


So tanks under 80000 will become RED, 80000 to 399999 will become YELLOW and from 400000 become GREEN.
If you need more information, please let me know.

File Link: https://www.dropbox.com/s/l420pebydge1nm7/Tanks.xlsm?dl=0


Thanks
 
Upvote 0
Try:
Code:
Private Sub Worksheet_Calculate()
    Static LastValueJ, LastValueH
    Dim shp As Shape
    If IsNumeric(Range("J24")) Then
        For Each shp In ActiveSheet.Shapes
            If shp.Name Like "Tank*" Then
                If Range("J24").Value < 80000 Then
                   shp.Fill.ForeColor.RGB = vbRed
                ElseIf Range("J24").Value >= 80000 And Range("J24").Value < 400000 Then
                   shp.Fill.ForeColor.RGB = vbYellow
                Else
                    shp.Fill.ForeColor.RGB = vbGreen
                End If
            End If
        Next shp
    End If

    With Range("j24")
        If LastValueJ <> .Value Then
            AdjustTank .Value, "A"
            LastValueJ = .Value
        End If
    End With
    With Range("H24")
        If LastValueH <> .Value Then
            AdjustTank .Value, "B"
            LastValueH = .Value
        End If
    End With
End Sub
 
Upvote 0
Hi mumps,
Tank A is working good.
The problem in Tank B is that when Tank A becomes yellow, Tank B appears as yellow and if Tank A becomes red, Tank B appears as red.
To give you an example if Tank B has 40000 and Tank A has 100000 both became yellow. In this example Tank B must appears in red.


mumps when comparing between us I'm very far from practising.


Thanks in advance
 
Upvote 0
Give this a try:
Code:
Private Sub Worksheet_Calculate()
    Static LastValueJ, LastValueH
    Dim shp As Shape
    
    For Each shp In ActiveSheet.Shapes
        If shp.Name = "TankA" Then
            If IsNumeric(Range("J24")) Then
                If Range("J24").Value < 80000 Then
                   shp.Fill.ForeColor.RGB = vbRed
                ElseIf Range("J24").Value >= 80000 And Range("J24").Value < 400000 Then
                   shp.Fill.ForeColor.RGB = vbYellow
                Else
                    shp.Fill.ForeColor.RGB = vbGreen
                End If
            End If
        ElseIf shp.Name = "TankB" Then
            If IsNumeric(Range("H24")) Then
                If Range("H24").Value < 80000 Then
                   shp.Fill.ForeColor.RGB = vbRed
                ElseIf Range("H24").Value >= 80000 And Range("H24").Value < 400000 Then
                   shp.Fill.ForeColor.RGB = vbYellow
                Else
                    shp.Fill.ForeColor.RGB = vbGreen
                End If
            End If
        End If
    Next shp

    With Range("j24")
        If LastValueJ <> .Value Then
            AdjustTank .Value, "A"
            LastValueJ = .Value
        End If
    End With
    With Range("H24")
        If LastValueH <> .Value Then
            AdjustTank .Value, "B"
            LastValueH = .Value
        End If
    End With
End Sub
 
Upvote 0
Hi mumps,
BULLSEYE
That means that you are PERFECT.
If you don't mind another question.
If I want to change color like from yellow to another color, what I have to do? I saw something like this =RGB(22,1,200)

Regards
 
Upvote 0
mumps
First of all thanks for the color code but I recognized that the level of the color of both tanks is not corresponding to the value.
Regards
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,484
Members
449,455
Latest member
jesski

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