Mike808
New Member
So, a great user on this forum posted in May of 2016 this great code for adding trendlines to a chart using VBA and looping to add the trendlines for all series applicable. My question is, how would I amend this code in the regular Module to make the trendlines occur on all four of my charts on the ActiveSheet? I know it is some kind of array or Dim/Set for the charts, but I can't figure it out!! For the code below....thanks much to user Chihiro:
It would be something like below in regular Module:
And then in Sheet2(Chart) module add:
This will add linear trend line to each series. Code also checks if there is trend line already present on the series. If it is, it will not add another.
It would be something like below in regular Module:
Code:
Sub AddTrendLine()
Dim mySeriesCol As SeriesCollection
Set mySeriesCol = ActiveSheet.ChartObjects(1).Chart.SeriesCollection
For i = 1 To mySeriesCol.Count
If mySeriesCol(i).Trendlines.Count > 0 Then
Else
mySeriesCol(i).Trendlines.Add
End If
Next
End Sub
And then in Sheet2(Chart) module add:
Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Call AddTrendLine
End Sub