当我更改图形数据时,有时会发生excel中的trendline标签未更新。因此,我想通过VBA更新。我想对所有工作表和图表中的所有现有趋势线进行此操作。
到目前为止,我的代码无效。您将在评论中找到错误。
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
########################################################################
存在运行时错误'438'。对象不支持带有错误注释的行中的此属性或方法。
答案 0 :(得分:0)
中有错误
ActiveChart.SeriesCollection(1).Trendlines(1).Select
将其更改为:
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
答案 1 :(得分:0)
以下是解决方案: :)
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