How To Make Animated Graphs in Excel
How To Make Animated Graphs in Excel
This tutorial will walk you through the steps of creating a simple animated graph in Excel. The end product is an animated scatter plot (similar to Gapminder World), but the basic technique can be extended to other types of graphs. This document lays out the steps for creating the graph. For the finished product, please see the accompanying Excel workbook. This technique does involve a small bit of code to manipulate the value of one cell in the workbook, but otherwise everything is done using worksheet functions. As you go through this tutorial, I suggest that you start by following the example exactly, using the same data and the same cells, because I make specific cell references in the text. Once you understand the concept, you can extend it to work for your own data.
A 1 2 3 4 5 6 7 8 9 B 2000 x values Male 112.27 91.0834 105.1893 82.13195 y values Female 103.08 63.164 96.958 71.051 C
1. Format your data as shown at left. You can have more than four
countries or regions if you want, as long as you have a column of x values and a column of y values for each country. In this example, the x and y values correspond to male and female gross enrolment rate, respectively, but they could also correspond to urban and rural, or even to two different indicators (such as fertility rate and gross enrolment rate). We're going to start by making a regular x-y scatter plot chart, so the data that you are formatting should all be from the same year (2000 in this example).
A 1 2 3 4 5 6 7 8 9
20.00
40.00
60.00 Male
80.00
100.00
120.00
now we're going to add in the other years. Format your data like cells A12:L25 below (do not replace the x and y values that are currently being graphed).
A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 B 2000 y values Female 103.08 63.164 96.958 71.051 C D E F G H I J K L
Male Algeria Benin Egypt Gambia 1995 105.4 81.9 106.8 76.1 1996 106.2 83.7 108.8 79.3 1997 107.4 85.4 108.0 80.4 1998 108.6 87.1 107.2 81.6 1999 109.8 88.8 106.5 82.7 2000 112.3 91.1 105.2 82.1 2001 111.3 99.7 104.1 77.9 2002 113.0 104.8 104.0 78.9 2003 114.6 108.0 103.7 78.3 2004 115.7 110.7 103.5 73.3 2005 115.8 106.2 104.8 72.2
Female Algeria Benin Egypt Gambia 1995 93.3 46.9 92.2 57.7 1996 95.0 49.4 94.5 62.6 1997 96.8 52.7 95.4 65.6 1998 98.6 56.0 96.4 68.6 1999 100.3 59.3 97.3 71.5 2000 103.1 63.2 97.0 71.1 2001 102.4 70.3 97.0 72.2 2002 104.7 76.1 97.6 79.3 2003 106.1 80.4 98.2 78.3 2004 107.4 85.6 99.1 78.2 2005 107.3 84.8 98.2 77.6
5. The rest of this tutorial depends heavily on the Excel OFFSET() function. If you are not familiar with this
function, I would suggest reading about it in Excel's help menu or in the appendix of this document before continuing. The syntax for using this function is =OFFSET(reference cell, rows, columns, height, width), but that wont mean much to you unless you read the documentation about how to use the function.
A 1 2 3 4 5 6 7 8 9 B 1 2000 x values Male 112.27 91.0834 105.1893 82.13195 y values Female 103.08 63.164 96.958 71.051 C
6. Type the number 1 in the yellow cell (B1) above the year for the
data that is currently being graphed. This value will be used to determine which column in the table above (Data for all years) will be used in the graph. Eventually the value of this cell will be controlled with a bit of code, but well get to that later.
7. Click on the cell below the number 1, which currently says 2000 (B2). Enter an offset formula as
follows: =OFFSET(A13,0,B1). Hit "Enter" and the new value in the cell should be 1995. If its not, go back to Excels documentation and read about the OFFSET function again.
8. Change the value in the yellow cell to 2. The year below it should change to 1996. So by changing the
value of that one cell, we can change the value of the year. Our goal is to also change the values of the data in the x and y value columns. Click on cell B6 and enter an offset formula as follows: =OFFSET(A14,0,B$1). Hit Enter and the new value in the cell should be the value from cell C14. Make sure that the formula says B$1 instead of B1. This will allow you to fill in the rest of the formulas without changing the reference to the yellow cell.
9. Select cell B6 and move your mouse to the bottom right corner of the cell until the cursor turns into a black
cross. Click and drag down to fill in formulas for the rest of the male cells. Repeat the same steps to add formulas to the cells with female values.
10. Try changing the value in the yellow cell (B1): 3, 4, 5, etc. Changing this one cell should cause the year
below it to change, as well as the values in the x and y (male and female) columns. Since your graph is using these cells as its data source, this should also cause the dots on your graph to move.
11. Now we can add some ways of controlling the value in the yellow cell, other than just typing in a value.
One of the easiest things we can do is add a scrollbar. Go to View Toolbars Forms, if the Forms toolbar is not already visible. Select the scrollbar icon and click and drag to add a scrollbar to the worksheet. Rightclick on the scrollbar and choose "Properties..." Edit the "LinkedCell" field so that it refers to the yellow cell. Change the Min to be 1 and the Max to be 11 (because we have 11 years of data). Click on the blue triangle icon in the Forms toolbar to exit editing mode. You can now use this scrollbar to run through all of the years for which you have data.
years in sequence. For this, we will write a macro. Start by going to View Toolbars Controls, if the Controls toolbar is not already visible. The Controls toolbar is not the same as the Forms toolbar, which you used in step 11. If you create a button using the Forms toolbar, the following instructions wont work. Add a button to the worksheet from the Controls toolbar. Right-click on the button, and go to CommandButton Object Edit to change the text on the button.
12. You might also want to have a button that, when clicked, will cause the graph to run through all of the
13. Double click on the button. You will be taken to an empty function that will run every time the button is
clicked. Basically, you want some code that will change the value in the yellow cell (B1 in this example), wait for a fraction of a second, and then change it again, and so on, until the maximum value of 11 is reached. You can copy and paste this code into the function (between Private Sub CommandButton1_Click() and End Sub:
Dim PauseTime, Start Dim i As Integer i = 1
Do While i <= 11 Sheet1.Cells.Range("B1").Value = i i = i + 1 PauseTime = 0.3 ' Set duration. Start = Timer ' Set start time. Do While Timer < Start + PauseTime DoEvents ' Yield to other processes. Loop Loop
You may need to change Sheet1 and B1 so that they refer to the yellow cell in your workbook. PauseTime = 0.3 indicates that the macro pauses for 0.3 seconds for each year. If you want it to run faster, try 0.1 or 0.2.
14. If you want, you can add a second button that resets the chart back to the earliest year. Just add a button
as you added the Run button, and double-click on it. In the code for this new button, add this one line:
Private Sub CommandButton2_Click() Sheet1.Cells.Range("B1").Value = 1 End Sub
Appendix OFFSET
Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells. The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and the number of columns to be returned. Syntax OFFSET(reference, rows, cols, height, width) Reference is the reference from which you want to base the offset. Reference must refer to a cell or range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value. Rows is the number of rows, up or down, that you want the upper-left cell to refer to. Using 5 as the rows argument specifies that the upper-left cell in the reference is five rows below reference. Rows can be positive (which means below the starting reference) or negative (which means above the starting reference). Cols is the number of columns, to the left or right, that you want the upper-left cell of the result to refer to. Using 5 as the cols argument specifies that the upper-left cell in the reference is five columns to the right of reference. Cols can be positive (which means to the right of the starting reference) or negative (which means to the left of the starting reference). Height is the height, in number of rows, that you want the returned reference to be. Height must be a positive number. Width is the width, in number of columns, that you want the returned reference to be. Width must be a positive number. Remarks If rows and cols offset reference over the edge of the worksheet, OFFSET returns the #REF! error value. If height or width is omitted, it is assumed to be the same height or width as reference. OFFSET doesn't actually move any cells or change the selection; it just returns a reference. OFFSET can be used with any function expecting a reference argument. For example, the formula SUM(OFFSET(C2,1,2,3,1)) calculates the total value of a 3-row by 1-column range that is 1 row below and 2 columns to the right of cell C2.
Example A Formula =OFFSET(C3,2,3,1,1) =SUM(OFFSET(C3:E5,-1,0,3,3)) =OFFSET(C3:E5,0,-3,3,3) B Description (Result) Displays the value in cell F5 (0) Sums the range C2:E4 (0) Returns an error, because the reference is not on the worksheet (#REF!)
1 2 3 4