
Tips.Net > ExcelTips Home > Charts > Creating Charts > Automatically Creating Charts for Individual Rows in a Data Table
Summary: Got a huge amount of data in a data table, but you only want to select a single record from the table to create a chart? This tip presents three ways you can access just the information you want and create a dynamic chart based on that information. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
David Poduska has a worksheet that he uses to track sales by company over a number of months. The company names are in column A, and up to fifteen months of sales are in columns B:P. David would like to create a chart that could be dynamically changed to show the sales for a single company from the worksheet.
There are several ways that this can be done; I'll examine three of them in this tip. For the sake of example, let's assume that the worksheet is named MyData, and that the first row contains data headers. The company names are in the range A2:A151, and the sales data for those companies is in B2:P151.
One approach is to use Excel's AutoFilter capabilities. Create your chart as you normally would, making sure that the chart is configured to draw its data series from the rows of the MyData worksheet. You should also place the chart on its own sheet.
Now, select A1 on MyData and choose Data | Filter | AutoFilter. A small drop-down arrow appears at the top of each column. Click the drop-down arrow for column A and select the company you want to view in the chart. Excel redraws the chart to include only the single company.
The only potential drawback to the AutoFilter approach is that each company is considered an independent data series, even though only one of them is displayed in the chart. Because they are independent, each company is charted in a different color. If you want the same charting colors to always be used, then you will need to use one of the other approaches.
Another way to approach the problem is through the use of an "intermediate" data table--one that is created dynamically, pulling only the information you want from the larger data table. The chart is then based on the dynamic intermediate table. Follow these steps:
=INDEX(MyData!A2:A151,$A$1)
="Data for " & A3
=ChartData!$A$3
You now have a fully functioning dynamic chart. You can use the Combo Box to select a different company, and the chart is redrawn using the data for the company you select. If you want, you can move or copy the Combo Box to the sheet containing your chart so that you can view the updated chart every time you make a selection. You can also, if desired, hide the ChartData worksheet.
A third approach is to use a macro to modify the range on which a chart is based. To prepare for this approach, create two named ranges in your workbook (Insert | Name | Define). The first name should be ChartTitle, and it should refer to the formula =OFFSET(MyData!$A$1,22,0,1,1) Click Add, and then define the second name. This one should be called ChartXRange, and it should refer to the formula =OFFSET(MyData!$A$1,22,0,1,15). (Click here to see a related figure.)
With the names defined, you can go ahead and create your chart. Select the range MyData!B1:P2 and run the Chart Wizard. You should base the chart on this simple range, and make sure that you place some temporary text in the chart title. Make sure the chart is created on its own sheet and that you name the sheet ChartSheet.
With the chart created, right-click the chart and choose Source Data. Excel displays the Source Data dialog box, and you should choose the Series tab. Since you based the chart on the headers and a single row of data, there should be a single data series in the dialog box. Replace whatever is in the Values box with the following formula:
='Book1'!ChartXRange
Make sure you replace Book1 with the name of the workbook in which you are working. Click OK, and the chart is now based on the named range you specified earlier. You can now select the chart title and place the following in the Formula bar to make the title dynamic:
=MyData!ChartTitle
Now you are ready to add the macro that makes everything dynamic. Display the VBA Editor and add the following macro to the code window for the MyData worksheet. (Double-click the worksheet name in the Project Explorer area.)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
ActiveWorkbook.Names.Add Name:="ChartXRange", _
RefersToR1C1:="=OFFSET(MyData!R1C1," & _
ActiveCell.Row - 1 & ",1,1,15)"
ActiveWorkbook.Names.Add Name:="ChartTitle", _
RefersToR1C1:="=OFFSET(MyData!R1C1," & _
ActiveCell.Row - 1 & ",0,1,1)"
Sheets("ChartSheet").Activate
Cancel = True
End Sub
Now you can display the MyData worksheet and double-click any row. (Well, double-click in column A for a row.) The macro then updates the named ranges so that they point to the row on which you double-clicked, and then displays the ChartSheet sheet. The chart (and title) are redrawn to reflect the data in the row on which you double-clicked.
Tip #2377 applies to Microsoft Excel versions: 97 2000 2002 2003
Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
Check out ExcelTips Premium today!
Thousands of ExcelTips, available for immediate download. Have all the Microsoft Excel info you need, right at your fingertips. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site