Create a bubble pie chart or world map pie chart using vba and Excel charts

We all know what a pie chart looks like, and we all know what a bubble chart looks like:Pie & bubble

What if you could combine the two to make a bubble pie chart. (Thanks to work done by Andy Pope):Bubble Pie ChartOr even using longitudes and latitudes, create a world map pie chart:

worldmap graphHow can it be done?  If you have the new Excel 2013, then it’s easy: use the new extensions.  If you don’t, then to create a simple bubble pie chart there are 5 steps:

  1. Create the data table for the different pie charts
  2. Create 1 sample pie chart
  3. Include the bubble graph data in the data table
  4. Create the bubble graph
  5. Add the vba code to add the pie charts to the bubble graph

 

Download the example excel sheet here

Step 1: Create the data table for the different pie charts.

Imagine you have a data table like the one shown below.  (Please note the three blank columns, we’ll fill those in in Step 3). Each row of data represents a pie chart you wish to create with 6 segments per pie chart.  You should be able to see this table on the basic tab of the spreadsheet above.  Create a named range by highlighting the numbers in your data table and then typing the new name “PieChartValues” in the name box.Named range

Step 2: Create one sample pie chart.

Using the Data 1 row, create a pie chart.  Remove the fill of the chart and the outline, leaving only the six segments of the pie chart.  Open up the selection pane (Home > Find and select > Selection pane) and rename the graph “chtMarker”. Format the segments as you wish:pie1

Step 3: Include the bubble graph data in the data table.

Add the data to the 3 columns of your data table as shown below (X, Y and Size):full table

The X and Y columns will be used by Excel to plot your bubbles on the graph, the Size column tells Excel the size of the bubble

Step 4: Create the bubble graph.

Highlight the columns B, C & D of your data table, and insert a bubble graph (Click on Insert > Other graphs > Bubble).

Excel will automatically format the bubbles for you.  Adjust the axes and the formatting of the graph as you wish.  Open up the selection pane (Home > Find and select > Selection pane) and rename the graph “chtMain”bubble 1

Step 5: Add the vba code to add the pie charts to the bubble graph.

Add the vba code below in Module 1.

Sub PieMarkers()

Dim chtMarker As Chart
Dim chtMain As Chart
Dim intPoint As Integer
Dim rngRow As Range
Dim lngPointIndex As Long

Application.ScreenUpdating = False
Set chtMarker = ActiveSheet.ChartObjects(“chtMarker”).Chart
Set chtMain = ActiveSheet.ChartObjects(“chtMain”).Chart

Set chtMain = ActiveSheet.ChartObjects(“chtMain”).Chart
Set rngRow = Range(ThisWorkbook.Names(“PieChartValues”).RefersTo)

For Each rngRow In Range(“PieChartValues”).Rows

chtMarker.SeriesCollection(1).Values = rngRow
chtMarker.Parent.CopyPicture xlScreen, xlPicture
lngPointIndex = lngPointIndex + 1
chtMain.SeriesCollection(1).Points(lngPointIndex).Paste

Next

lngPointIndex = 0

Application.ScreenUpdating = True

End Sub

Please note, you must save the file with the extension *.xlsm if you want the macros to work.  You can also easily substitute the named range if you wish, by simply typing in the cell reference e.g. (“E4:J12”) instead of (“PieChartValues”).

Create a button on the page using the developer tab and assign the macro above to it.  Then click the button.  Adjust the data as you wish and click the button again.  The chart should update.

World Map

If you wish to create a world map like the one shown on the “WorldMap” tab of the spreadsheet attached here, all you need to do is change Columns B and C to longitude and latitude coordinates:

worldmap data

If you don’t know the longitudes and latitudes of the places where the charts need to be, use Google Maps and iTouchMap.com.  You can drag the marker to the position you wish on Google maps and it will give you the longitude and latitude

iTouchMaps

 

Then format the plot area of the graph and add a picture showing the map you wish.format plot area

Adjust the X and Y axis to match the maximum and minimum longitude and latitude shown on the graph. worldmap graph

 

Enjoy!

Alesandra Blakeston

13 thoughts on “Create a bubble pie chart or world map pie chart using vba and Excel charts

  1. When I try to run the code, the range for the Data 1 values changes to the last row. So I get a pie chart for Data 1 but with Data 9 values. Moreover, the pie charts that are added to the graph looks more wedged in. It doesn’t matter how much the size is increased, it has a white background making the chart look tiny.. like a bean shape. Any idea on this?

  2. Hi, I keep receiving a Run-time error 1004: Application-defined or object-defined error. Any help?

    1. You get that error when there is a problem with the code. I would imagine that you have changed some of the variables or the names in the sheet and now the code cannot find what it is looking for. Check the name manager in the first instance and then check the variables listed in the code to make sure you haven’t changed anything important while editing the sheet

  3. I had this same issue but resolved it when I replaced the quotation marks. When I directly copied your code into visual basic, the quotes copied incorrectly but when I pasted over them manually, the code worked perfectly. Thank you for this great solution!

  4. Lovely! If your bubble-icons look less than round, you can adjust by clicking and dragging on the corners of your initial bubble chart to get the height:width ratio you want.

Leave a comment