Sunteți pe pagina 1din 17

This workbook contains VBA code that uses the ChartSeries class module

The ChartSeries class module makes it easy to work with chart data using VBA. The class module may be exported and then imported into any project. Click the sheet tabs to access the demos, press Alt+F11 to view the code. l Demo1: VBA code is in the code module for the Demo1 sheet l Demo2: VBA code is in the code module for the Demo2 sheet l Demo3: VBA code is in the code module for the Demo3 sheet l Demo4: VBA code is in Module1 Developed by John Walkenbach, JWALK AND ASSOCIATES
1999. All rights reserved. May be used and distributed freely, but may not be sold.

http://www.j-walk.com/ss/

Excel's object model has a serious flaw: There is no direct way to to use VBA to determine the ranges used in a chart. This tip contains a useful class module that can simplify the task of manipulatin g chart's using VBA.

Backgr ound Inform ation

The Series object is contained in a Chart object. The SeriesCollec tion is a collection of Series objects for a particular Chart object. If a chart plots two data series it will have two Series objects. You can refer to a particular Series object by its index number. The expression below, for example, creates an object variable that represents the first Series object in the active chart: Set MySeries = ActiveChart.SeriesCollection(1) A Series object has many properties, but I'll list three that seem relevant to this discussion:

Formula property: Returns or sets the SERIES formula for the Series. When you select a series in a chart, its SERIES formula is displayed in the formula bar. The Formula property returns this formula as a string.

Values property: Returns or sets a collection of all the values in the series. This can be a range on a worksheet or an array of constant values, but not a combinati on of both.

XValues property: Returns or sets an array of x values for a chart series. The XValues property can be set to a range on a worksheet or to an array of values -but it can't be a combinati on of both.

Values property of the Series object is just the ticket. And, you can use the XValues property to get the range that contains the x values (or category labels). In theory, that certainly seems correct -but in practice, it doesn't work. You'll find that he XValues and Values properties return a variant array. Unfortunatel y, there is no direct way to get a Range object for a Series object.

Note: When you set the Values property for a Series object, you can specify a Range object or an array. But when you read this property, it is always an array. Go figure.

About a Chart's SERIES formul a


Every data series in a chart has a SERIES formula that determines the data used in the series. Here's an example of a SERIES formula for a chart series: =SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$13,Sheet1!$B$2:$B$13,1) A SERIES formula is comprised of four arguments: Series Name. Can be a cell reference or a literal string. Optional. XValues. Can be a range reference (including a noncontiguou s range reference) or an array. Optional.

Values. Can be a range reference (including a noncontiguou s range reference) or an array. Plot Order. Must be an integer. The ChartSeries calls module, described below, essentially parses and analyzes a chart's SERIES formula.

The ChartS eries Class Module

I created a class module named ChartSeries. When this class module is included in a workbook, your VBA code can create a new "ChartSeries " object and manipulate the following properties of this object: Chart (read/writ e) ChartSerie s (read/writ e) SeriesNa me (read/writ e) XValues (read/writ e) Values (read/writ e) PlotOrder (read/writ e) SeriesNa meType (readonly) XValuesTy pe (readonly) ValuesTyp e (readonly)

PlotOrder Type (readonly)

Exampl e Usage class. It

starts by creating a new object called ChartSeries. It sets the Chart property to an embedded chart, and sets the ChartSeries property to 1. It then uses the XValuesType property to determine the "type" of the XValues in the chart. If the chart uses a range, it displays the address of the range. If the chart uses something other than a range (that is, a literal array), it displays the array. Sub ExampleUsage() Dim MySeries As New ChartSeries With MySeries Chart = Sheets(1).ChartObjects(1).Chart ChartSeries = 1 If .XValuesType = "Range" Then MsgBox .XValues.Address Else MsgBox .XValues End If

End With End Sub

Month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

Last Yr This Yr 183 144 109 132 144 155 162 200 120 232 172 255 194 301 119 277 128 322 174 195 157

350 300 250 200 150 100 50 0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Last Yr This Yr

Series Name Values

XValues Plot Order

Last Year This Year

Aug

Sep

Oct

Nov

Dec

Year Year

Month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb

Sales 43 55 64 54 78 69 75 72 81 92 84 102 104 90 70 60 50 40 30 20 10 0 Jan Fewer Months More Months Feb Mar

Mar

Label Data 1 Data 2 Data 3 Data 4 Data 5 Data 6 Data 7 Data 8 Data 9 Data 10 Data 11 Data 12 Data 13 Data 14 Data 15 Data 16 Data 17 Data 18 Data 19 Data 20 Data 21 Data 22 Data 23 Data 24 Data 25 Data 26

Data 183 109 144 162 120 172 194 119 128 174 195 157 109 149 194 183 177 160 159 163 185 121 121 114 107 128
250 200 150 100 50 0
Data 4 Data 5 Data 6 Data 7 Data 8 Data 9 Data 10 Data 11 Data 12

Select cells in column B. Then click here.

Data 10

Data 11

Data 12

A Chart That Uses Arrays

10 9 8 7 6 5 4 3 2 1 0

S-ar putea să vă placă și