Charting data is an easy task and reusing a chart is efficient! By reusing, I mean to use the same chart object to chart multiple sources. In other words, I'm going to show you how to create a dynamic chart. They're common in dashboards and you might be surprised to learn how easy they are to implement.
Instead of launching right into the technique, let's take a quick high-end look at what this technique entails, because it has several steps:
- Add a dynamic column to the data range using a HLOOKUP() function. This column will be the chart's real source, not the data range.
- Insert a simple chart.
- Add a scroll bar control that lets the user easily update the chart's source, without really knowing what's going on behind the scenes.
Now, let's chart the data shown below. Specially, we'll create a simple bar chart that displays the sales for each salesperson, by year. Ordinarily, you might think that you need four different charts, but that's where our dynamic solution comes in- we'll use the same chart object to show all four years.
In the first step, add a column of HLOOKUP() functions, as follows:
- In cell F2, enter a year from the headings. For instance, enter 2009.
- In cell F3, enter the following function: =HLOOKUP($F$2,$B$2:$E$6,2). Be sure to note the absolute addresses—they're vital!
- Copy the function to cells F4:F6. The functions in column F return the appropriate values for the year identified in F2. In the following sheet, that's 2009.
- In the copied formulas, update the last argument from 2 to 3, 4, and 5, respectively. (F4 is 3, F5 is 4, and F6 is 5.)
The next step is to insert a chart based on the personnel values in column A and the source column (F), as follows:
- Select A3:A6.
- Hold down [Ctrl].
- Select F3:F6. You just created a non-contiguous selection.
- Click the Insert tab.
- In the Charts group, click the Bar dropdown.
- Choose the first 2-D cluster bar chart option.
- In the resulting chart, delete the legend - just click it and press Delete. You don't need it.
Changing the year value in F2 updates the chart. Go ahead and try it. Enter the year values 2008 through 2011 and watch the chart update automatically!
As is, it's a handy chart, but users won't know to update the value in F2. You need to provide a more intuitive method. We'll use a scroll bar Form control, as follows:
- Click the Developer tab.
- In the Controls group, click the Insert dropdown.
- Click Scroll Bar (third control on the second line).
- Click inside the sheet to insert the Form control.
- Right-click the newly inserted control and choose Format Control.
- Click the Control tab (if necessary).
- Enter the following values: 2008, 2008, 2011, and F2 as the linking cell (as shown below).
- Next, drag the current year value in F2 and drop it near the scroll bar (see below as a guide). This last step isn't necessary, but doing so will help the user understand what the scroll bar is for. Reformat the linking cell if you like (I chose not to so you could easily see that I dragged the cell from F2. You'd probably want to reformat it in a production workbook.)
Simply click the scroll bar arrows to increment its values from 2008 to 2011 - those are the controls minimum and maximum values, incrementing by 1. The chart will update accordingly. Using the scroll bar isn't necessary, but it will make things much easier for your users, especially if you separate the chart from the actual data range in a dashboard type setting.
Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.