SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 198 | Next

Jim DeMarco

"Pro Excel 2007 VBA"


2. If it??™s not already open, open Standard Module1.
3. Copy the MakePieChart macro.
4. Paste the copy below MakePieChart and rename it MakePieChart2.
5. Modify all range references to refer to the data range containing the Beverage category
sales information, as shown in Listing 5-2.
Listing 5-2.MakePieChart2 Subroutine Modified to Chart the Beverage Category
Sub MakePieChart2()
Range("A6:C9").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sales By Category'!$A$6:$C$9")
ActiveChart.ChartType = xlPie
ActiveChart.SeriesCollection(1).Name = "='Sales By Category'!$A$6"
ActiveChart.SeriesCollection(1).XValues = "='Sales By Category'!$B$6:$B$9"
End Sub
CHAPTER 5 n CHARTING IN EXCEL 2007 212
As in our original example, we are selecting a range of data (A6:C9), and then adding a
chart and setting the source data range to the selected range. Then we set the chart
type to Pie (xlPie) and set the name and legend values.
6. Run the MakePieChart2 macro.
As shown in Figure 5-29, Excel still insists on placing the pie chart on top of our data
range. In fact, if we had not moved the Baked Goods & Mixes pie chart, the new chart would
be sitting on top of it (and it still is partially covering our existing chart)!
Figure 5-29. Excel places any new chart on our data range.
Not to worry. You??™ll recall that when we created our first chart using the Macro Recorder,
Excel used the AddChart method to insert the chart. We looked at the optional arguments for
that method in Table 5-1.


Pages:
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210