VBA change colour of icon (graphic) based on cell value

drefiek2

New Member
Joined
Apr 23, 2023
Messages
48
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi, I need help with a code which will change the colour of an icon (Graphic 9) to a different colour (detailed below in RBG format), depending on whether cell U5 has the following text:
"NIL" - red 0, green 176, blue 80
"LOW" - red 255, green 192, blue 0
"MED" - red 198, green 89, blue 17
"HIGH" - red 255, green 0, blue 0

If this will not work with icon graphics, is it worth converting them to a shape? Any help would be much appreciated, thank you.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How and where did you get Graphic Icon?
Insert > Icons
It shows in the Name Box as 'Graphic #'

I can convert the icon to a shape if VBA will not work with icons/graphics.

Screenshot 2023-04-27 185556.png
 
Upvote 0
I figured it out. Note the RGB values can be changed to get the colour you want!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("U5") = "NIL" Then
ActiveSheet.Shapes.Range(Array("Graphic 57")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
If Range("U5") = "LOW" Then
ActiveSheet.Shapes.Range(Array("Graphic 57")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0)
Else
If Range("U5") = "MED" Then
ActiveSheet.Shapes.Range(Array("Graphic 57")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 0, 255)
Else
If Range("U5") = "HIGH" Then
ActiveSheet.Shapes.Range(Array("Graphic 57")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 239, 0)
End If
End If
End If
End If
End Sub
 
Upvote 0
Solution
Nice to hear it works.
However your code should be in neater and shorter way with "With" and "Select Case"
Like this:
PHP:
ActiveSheet.Shapes.Range(Array("Graphic 57")).Select
With Selection
     Select case  Range("U5") 
          Case  "NIL"
                     ShapeRange.Fill.ForeColor.RGB = RGB( 0, 255, 0)
          Case  "LOW"
                     ShapeRange.Fill.ForeColor.RGB = RGB( 0, 0, 255)
          Case  "MED"
                     ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
          Case  "HIGH"
                     ShapeRange.Fill.ForeColor.RGB = RGB(255, 239, 0)
     End Select
End With
 
Upvote 0
Nice to hear it works.
However your code should be in neater and shorter way with "With" and "Select Case"
Like this:
PHP:
ActiveSheet.Shapes.Range(Array("Graphic 57")).Select
With Selection
     Select case  Range("U5")
          Case  "NIL"
                     ShapeRange.Fill.ForeColor.RGB = RGB( 0, 255, 0)
          Case  "LOW"
                     ShapeRange.Fill.ForeColor.RGB = RGB( 0, 0, 255)
          Case  "MED"
                     ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
          Case  "HIGH"
                     ShapeRange.Fill.ForeColor.RGB = RGB(255, 239, 0)
     End Select
End With
Hi there!
I did try it quickly but it did not work, it doesn't change the graphic at all. I am just a beginner with VBA so I am trying to learn how to improve codes :)
 
Upvote 0
Try, instead of
Select case Range("U5")

replace with
Select case Range("U5").Value
???
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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