Change Shape Color based on Cell Value (Excel VBA)

Valeriew

New Member
Joined
Oct 26, 2021
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
I am new to VBA and I want to make an interactive map consisting of 10 districts, where each district would be colored, for example, green if person A is assigned to that district or red if person B is assigned to that district . I used this code from other threads and there is no error in the formula, but it seems like the shape color doesn't change. Can anyone tell me what is wrong in the code?

Private Sub Worksheet_Change(ByVal Target As Range)

For i = 1 To 10

If Sheet31.Cells(i, 2) = "Person A" Then
Sheet31.Shapes.Range(Array("Praha_" & i)).Fill.ForeColor.RGB = RGB(237, 125, 49)
ElseIf Sheet31.Cells(i, 2) = "Person B" Then
Sheet31.Shapes.Range(Array("Praha_" & i)).Fill.ForeColor.RGB = RGB(255, 192, 0)
ElseIf Sheet31.Cells(i, 2) = "Person C" Then
Sheet31.Shapes.Range(Array("Praha_" & i)).Fill.ForeColor.RGB = RGB(91, 155, 23)
ElseIf Sheet31.Cells(i, 2) = "Person D" Then
Sheet31.Shapes.Range(Array("Praha_" & i)).Fill.ForeColor.RGB = RGB(165, 165, 165)
ElseIf Sheet31.Cells(i, 2) = "Person E" Then
Sheet31.Shapes.Range(Array("Praha_" & i)).Fill.ForeColor.RGB = RGB(96, 165, 165)

End If
Next i

End Sub
 

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

As a Shape is not a VBA event procedure so same question again : where is located this code, in which module ?!​
 
Upvote 0
By the way, you can just use Sheet31.Shapes("Praha_" & i) rather than Sheet31.Shapes.Range(Array("Praha_" & i))
 
Upvote 0
Screenshot 2021-10-27 173955.png
As a Shape is not a VBA event procedure so same question again : where is located this code, in which module ?!​
Sorry for not understanding your previous question correctly. This code is located in Worksheet 31 and not in any module (please refer to picture attached), I see similar codes are also applied in specific worksheet instead of module?
 
Upvote 0
As it's a worsheet event procedure so it must be located in the appropriate worksheet module which seems the case here.​
Let's check your need : when a name is updated in a cell within the range B1:B10 the color of the relative shape according to the row index must change​
if the entry is between Person A & Person E, am I right ?​
What is the default color when nobody is assigned to a district ?​
 
Upvote 0
As a reminder your original code is case sensitive …​
 
Upvote 0
As it's a worsheet event procedure so it must be located in the appropriate worksheet module which seems the case here.​
Let's check your need : when a name is updated in a cell within the range B1:B10 the color of the relative shape according to the row index must change​
if the entry is between Person A & Person E, am I right ?​
What is the default color when nobody is assigned to a district ?​

I uploaded dummy data on google drive for you to visualize it easier.

Yes, you are correct. The input is between Person A to Person E (in a drop-down list); for example if A1 (Praha_1) is assigned to Person B in B2, then the shape with the name Praha_1 should change color to RGB(255, 192, 0). If I change it again to Person A, then the color of Praha_1 shape should change to RGB(237, 125, 49).

The default color when nobody is assigned to a district should be RGB(255,255,255).
 
Upvote 0

According to your attachment, evaluate this idea : instead of hardcoding colors and names,​
as you are using a drop-down list in Sheet2 so using the Sheet2 column B for the color should be better like "237,125,49" ?​
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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