Mike808
New Member
With help from Hui last night, I was able to figure out how to add trendlines to my chart page in my workbook. I have the identical (at least I thought) chart page in another spreadsheet and wanted to add the VBA to the xlsm. Now I am getting a runtime error in the "mySeriesCol(j).Trendlines.Add line. Need help. Seriously have no idea why I am getting the runtime error, same exact charts and data, just on a different workbook.
Code:
Sub AddTrendLine()
Dim mySeriesCol As SeriesCollection
Dim sh As Worksheet
Dim i As Long, j As Integer
For Each sh In Worksheets
If sh.ChartObjects.Count > 0 Then
For i = 1 To sh.ChartObjects.Count
Debug.Print sh.ChartObjects(i).Chart.Name
Set mySeriesCol = sh.ChartObjects(i).Chart.SeriesCollection
For j = 1 To mySeriesCol.Count
If mySeriesCol(j).Trendlines.Count > 0 Then
Else
mySeriesCol(j).Trendlines.Add
End If
Next j
Next i
End If
Next sh
End Sub