Name

trendlines.Add([Type], [Order], [Period], [Forward], [Backward], [Intercept], [DisplayEquation], [DisplayRSquared], [Name])

Synopsis

Adds a trendline to a series.

Argument

Settings

Type

An xlTrendlineType constant indicating how the trendline is calculated. Can be one of these settings: xlExponential, xlLinear (default), xlLogarithmic, xlMovingAvg, xlPolynomial, or xlPower.

Order

For polynomial trendlines, the trendline order from 2 to 6.

Period

For moving average trendlines, the number of data points to include in the average.

Forward

The number of periods or units to project the trendline forward beyond the plotted series. Default is 0.

Backward

The number of periods or units to project the trendline backward beyond the plotted series. Default is 0.

Intercept

Where the trendline crosses the x-axis. If this argument is omitted, the intercept is automatically set by the regression.

DisplayEquation

True displays the trendline formula as a data label; False does not. Default is False.

DisplayRSquared

True displays the trendline R-squared value as a data label; False does not. Default is False.

Name

The text to display in the chart legend for the trendline. Default is a brief, generated description of the trendline.

The following code adds a polynomial trendline to the first series in a chart and forecasts the trend three units ahead of the last data point:

Sub AddPolyTrendline( ) Dim chrt As Chart, tl As Trendline, tls As Trendlines ' Get the ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.