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 191 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

ChartType = xl3DColumnClustered
The ChartType property is the same property from the optional arguments of the AddChart
method we saw in the second line of this macro. I??™ve always been a proponent of using less
code when possible. You could shorten the MakeBeverageSalesChart subroutine by leaving the
direct assignment of the ChartType property out and setting the ChartType when calling the
AddChart method.
The modified version of this code looks like Listing 5-1.
Listing 5-1.Modified MakeBeverageSalesChart Macro
Sub MakeBeverageSalesChart()
'
' MakeBeverageSalesChart Macro
'
'
Range("A1:E7").Select
ActiveSheet.Shapes.AddChart(xl3DColumnClustered).Select
ActiveChart.SetSourceData Source:=Range( ??
"'Monthly Total Sales Amount'!$A$1:$E$7")
End Sub
Now let??™s look at the code we created to switch the chart data orientation from column to
row in the ChartByRow macro:
Sub ChartByRow()
'
' ChartByRow Macro
'
'
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotBy = xlRows
End Sub
You??™ll recall the first thing we did was select our chart. The first line of this code calls the
ChartObjects.Activate method to activate the chart named Chart 1 (the default name given
to our chart). A ChartObject represents a chart embedded on a worksheet. The ChartObjects
object contains a collection of all the ChartObject objects on a chart sheet, dialog sheet, or
worksheet. (I realize the word object was used an awful lot in that last sentence, but let me
remind you that I did not name these objects!)
Like any other Collection object, ChartObjects in our ChartByRow macro refers to
Chart 1 by name, but it also could have referred to it by its index in the collection, as follows:
ActiveSheet.


Pages:
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203