0

有时,当我更改图形数据时,excel 中的趋势线标签没有更新。因此我想通过 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”。错误注释所在行中的对象不支持此属性或方法。

4

2 回答 2

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
于 2013-03-12T19:46:14.850 回答
0

有错误

ActiveChart.SeriesCollection(1).Trendlines(1).Select

将其更改为:

ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select

于 2013-03-07T18:49:36.277 回答