The Select Data Source dialog box should look like Figure 5-26.
CHAPTER 5 n CHARTING IN EXCEL 2007 209
Figure 5-26. The Select Data Source dialog after edits
15. Click OK to close the Select Data Source dialog box and save the changes to the chart.
16. Stop the Macro Recorder.
Figure 5-27 shows the updated chart with the category as the chart title and the month
names for the legend.
Figure 5-27. The updated pie chart
A Look at the Code
Now let??™s take a look at the code behind the process we just walked through. As you might
expect, the first few lines look very similar to the MakeBeverageSalesChart macro, right up to
the point where we set the chart type:
CHAPTER 5 n CHARTING IN EXCEL 2007 210
Sub MakePieChart()
'
' MakePieChart Macro
'
'
Range("A2:C5").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sales By Category'!$A$2:$C$5")
ActiveChart.ChartType = xlPie
ActiveChart.SeriesCollection(1).Name = "='Sales By Category'!$A$2"
ActiveChart.SeriesCollection(1).XValues = "='Sales By Category'!$B$2:$B$5"
End Sub
The two lines of code following ActiveChart.ChartType = xlPie, where we set the chart
type, define the name or title of the chart and the legend values (in this case the range B2:B5).
Let??™s look at the line of code that sets the name of the data series in our pie chart:
ActiveChart.SeriesCollection(1).Name = "='Sales By Category'!$A$2"
The SeriesCollection(index) object collection contains the data series for the chart.
Pages:
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208