Trendlines Collection Object

Microsoft Excel Visual Basic

Trendlines Collection Object

         
SeriesCollection (Series) Trendlines (Trendline)
Border
DataLabel

A collection of all the Trendline objects for the specified series. Each Trendline object represents a trendline in a chart. A trendline shows the trend, or direction, of data in a series.

Using the Trendlines Collection

Use the Trendlines method to return the Trendlines collection. The following example displays the number of trendlines for series one in Chart1.

MsgBox Charts(1).SeriesCollection(1).Trendlines.Count

Use the Add method to create a new trendline and add it to the series. The following example adds a linear trendline to the first series in embedded chart one on Sheet1.

Worksheets("sheet1").ChartObjects(1).Chart.SeriesCollection(1) _
    .Trendlines.Add type:=xlLinear, name:="Linear Trend"

Use Trendlines(index), where index is the trendline index number, to return a single TrendLine object. The following example changes the trendline type for the first series in embedded chart one on worksheet one. If the series has no trendline, this example will fail.

Worksheets(1).ChartObjects(1).Chart. _
    SeriesCollection(1).Trendlines(1).Type = xlMovingAvg

The index number denotes the order in which the trendlines were added to the series. Trendlines(1) is the first trendline added to the series, and Trendlines(Trendlines.Count) is the last one added.