sometimes it happens that the trendline label in excel isn't updated when I changed the graph-data. Therefore I want to update via VBA. I want to do it for all existing trendlines in all sheets and charts.
My code until now doesn't work. You will find the error in the comment.
Sub Auto_Open()
Debug.Print "Start"
Dim oChart As ChartObject, nSheet As Integer, nChart As Integer
nSheet = 1
Do While nSheet <= Sheets.Count
nChart = 1
Do While nChart <= Sheets(nSheet).ChartObjects.Count
nSeriesCollection = 1
'Debug.Print Sheets(nSheet).ChartObjects(nChart).SeriesCollection.Count
'Error in next line
Do While nSeriesCollection <= Sheets(nSheet).ChartObjects(nChart).SeriesCollection.Count
Debug.Print "nSheet: " & nSheet & " nChart: " & nChart
Set oChart = Sheets(nSheet).ChartObjects(nChart)
oChart.Activate
'Next line has to changed too
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.DisplayRSquared = False
.DisplayEquation = False
.DisplayRSquared = True
.DisplayEquation = True
End With
nSeriesColletion = nSeriesColletion + 1
Loop
nChart = nChart + 1
Loop
nSheet = nSheet + 1
Loop
End Sub
########################################################################
There is a run-time error '438'. Object doesn't support this property or method in the line with the error comment.
Here is the solution: :)
Sub Auto_Open()
Debug.Print "###########################################"
Debug.Print "Start"
Dim oChart As ChartObject, nSheet As Integer, nChart As Integer
nSheet = 1
Do While nSheet <= Sheets.Count
Debug.Print "Sheet: " & nSheet
nChart = 1
Do While nChart <= Sheets(nSheet).ChartObjects.Count
Debug.Print " ChartObjects: " & nChart
nSeriesCollection = 1
Do While nSeriesCollection <= Sheets(nSheet).ChartObjects(nChart).Chart.SeriesCollection.Count
Debug.Print " SeriesCollection: " & nSeriesCollection
Sheets(nSheet).ChartObjects(nChart).Chart.SeriesCollection(nSeriesCollection).Trendlines(1).DisplayEquation = True
nSeriesCollection = nSeriesCollection + 1
Loop
nChart = nChart + 1
Loop
nSheet = nSheet + 1
Loop
End Sub