Need to colour in shapes according to a value

hawkinsr86

New Member
Joined
Aug 10, 2012
Messages
24
Hi guys,

I have the a bunch of shapes I need fill with the colour green but I want the shade of green to be darker if the number is higher. Below is my data set. So for example I want Russia to be the greenest (because it is largest) and Luxembourg to be the least green etc...

Autoshape NameCountryPopulation
S_ALBAlbania3,639,453
S_AUTAustria8,210,281
S_BELBelgium10,414,336
S_BGRBulgaria7,204,687
S_BIHBosnia and Herzegovina4,613,414
S_BLRBelarus9,648,533
S_CHESwitzerland7,604,467
S_CZECzech Republic10,211,904
S_DEUGermany82,329,758
S_DNKDenmark5,500,510
S_ESPSpain40,525,002
S_ESTEstonia1,299,371
S_FINFinland5,250,275
S_FRAFrance64,057,792
S_GBRUnited Kingdom61,113,205
S_GRCGreece10,737,428
S_HRVCroatia4,489,409
S_HUNHungary9,905,596
S_IRLIreland4,203,200
S_ITAItaly58,126,212
S_LTULithuania3,555,179
S_LUXLuxembourg491,775
S_LVALatvia2,231,503
S_MDAMoldova4,320,748
S_MKDMacedonia2,066,718
S_NLDNetherlands16,715,999
S_NORNorway4,660,539
S_POLPoland38,482,919
S_PRTPortugal10,707,924
S_ROMRomania22,215,421
S_RUSRussia140,041,247
S_SRSerbia7,379,339
S_SVKSlovakia5,463,046
S_SVNSlovenia2,005,692
S_SWESweden9,059,651
S_UKRUkraine45,700,395

<colgroup><col span="3"></colgroup><tbody>
</tbody>

Could someone please help me with the code as I dont know where to start.

Thanks
Ryan
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi Ryan,

Here's a simple user-defined function (UDF) you can use to do this:

Code:
Function ColorByArea(ShapeName As String, Pop As Double) As Boolean
   'Fill a shape named ShapeName with color green according to the population.
   'Largest population (Russia) -> dark green   (140,041,247)
   'Smallest population (Luxembourg) -> very light green  (491,775)
   Dim Pfac    As Single   'area factor
   Dim RB      As Integer  'red & blue color values
   Dim G       As Integer  'green color value
   On Error GoTo NotFound
   Pfac = Sqr(Pop / 14041247#)
   RB = (1 - Pfac) * 255
   G = 255 - Pfac * 130
   With ActiveSheet.Shapes(ShapeName)
      .Fill.ForeColor.RGB = RGB(RB, G, RB)
   End With
   ColorByArea = True
   Exit Function
NotFound:
   ColorByArea = False
End Function

I did this as a function rather than a macro so that you could call it directly from your worksheet. You can use it like this. I'll assume your autoshape name, country, and population values are in columns A:C. In column D (in this example D2) of the corresponding rows place the formula

=ColorByArea(A2,C2)

where A2 is the shape name and C2 is the population value. This should color the shape green with lightness according to the corresponding population value.

Of course, this function can be called from a Sub procedure within VBA, but I assumed that calling it from a cell would be more convenient.

To install this function in your workbook go to the Visual Basic Editor (keyboard Alt-TMV, insert a new macro module (Alt-IM), and paste my code into the Code pane. It will be instantly available to use.
 
Upvote 0
Hi again Ryan,

I forgot to mention that the function returns TRUE if the named shape is found on the active worksheet, otherwise it returns FALSE.

Damon
 
Upvote 0
Hi Damon,

I did what you said but all the formulas are returning false for some reason. Any idea why?

Many Thanks
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,030
Members
448,940
Latest member
mdusw

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