Link to home
Start Free TrialLog in
Avatar of cheme1999
cheme1999

asked on

fix location of excel trendline

I have a chart that is automatically updated using VB to pull in new data. Each time the data is updated, there is a trendline applied that moves. How can I fix the location of the equation of the trendline using VB so that my printouts are always the same?
ASKER CERTIFIED SOLUTION
Avatar of byundt
byundt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cheme1999
cheme1999

ASKER

Is there a way to have the equation on the top left instead? I tried:

.DataLabel.Left = cht.Width - 65

and

.DataLabel.Left = cht.Width + 65

But neither worked. With the equation on the right, it overlaps my title.
I also tried
.DataLabel.Right = cht.Width - 65
.DataLabel.Right = cht.Width  + 65

But got an error.
One more thing...How can I process multiple charts in the same worksheet using this method? It seems to work fine for one chart. But, has trouble when I run through more.
.DataLabel.Left works from the left side of the chart, so setting it equal to a small number should accomplish your goal. You don't need to refer to cht.Width at all. To cycle through all the charts, use a For...Next loop.

Sub Macro1()
Dim cht As ChartObject
For Each cht In ActiveSheet.ChartObjects
    With cht.Chart.SeriesCollection(1).Trendlines(1)
       .DisplayRSquared = False
       .DisplayEquation = True
       .DataLabel.Left = 25
       .DataLabel.Top = 0
    End With
Next cht
End Sub
one more thing.....what if I have 12 charts, but only want to apply the macro to 4 of them?
byundt, I didn't mean to get carried away with asking more questions. However, if you have an answer for the last part, I'd really appreciate it!!!
If you only want to do four charts out of twelve, your best bet is to do them one at a time. Select the chart, then run the macro:

Sub Macro1()
With Selection
    With ActiveChart.SeriesCollection(1).Trendlines(1)
        .DisplayRSquared = False
        .DisplayEquation = True
        .DataLabel.Left = 25
        .DataLabel.Top = 0
    End With
End With
End Sub
Here's what I have with the mutliple charts:

        Sheets("Summary").ChartObjects(3).Select
        With Selection
           With ActiveChart.SeriesCollection(1).Trendlines(1) ''''I get an error that takes me to this line.
               .DisplayRSquared = False
               .DisplayEquation = True
               .DataLabel.Left = 25
               .DataLabel.Top = 0
           End With
        End With

What did I do wrong?
OK, here is another way to put the equation on just certain charts. Each of the charts on your worksheet has a name. Once you know it, then just add them to the line with Case "Chart 1", "Chart 2".

Sub aMacro1()
Dim cht As ChartObject
For Each cht In ActiveSheet.ChartObjects
    Select Case cht.Name
    Case "Chart 1", "Chart 3"
        With cht.Chart.SeriesCollection(1).Trendlines(1)
            .DisplayRSquared = False
            .DisplayEquation = True
            .DataLabel.Left = 25
            .DataLabel.Top = 0
        End With
    Case Else
    End Select
Next cht
End Sub