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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I also tried
.DataLabel.Right = cht.Width - 65
.DataLabel.Right = cht.Width + 65
But got an error.
.DataLabel.Right = cht.Width - 65
.DataLabel.Right = cht.Width + 65
But got an error.
ASKER
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).Trendl ines(1)
.DisplayRSquared = False
.DisplayEquation = True
.DataLabel.Left = 25
.DataLabel.Top = 0
End With
Next cht
End Sub
Sub Macro1()
Dim cht As ChartObject
For Each cht In ActiveSheet.ChartObjects
With cht.Chart.SeriesCollection
.DisplayRSquared = False
.DisplayEquation = True
.DataLabel.Left = 25
.DataLabel.Top = 0
End With
Next cht
End Sub
ASKER
one more thing.....what if I have 12 charts, but only want to apply the macro to 4 of them?
ASKER
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.SeriesCollecti on(1).Tren dlines(1)
.DisplayRSquared = False
.DisplayEquation = True
.DataLabel.Left = 25
.DataLabel.Top = 0
End With
End With
End Sub
Sub Macro1()
With Selection
With ActiveChart.SeriesCollecti
.DisplayRSquared = False
.DisplayEquation = True
.DataLabel.Left = 25
.DataLabel.Top = 0
End With
End With
End Sub
ASKER
Here's what I have with the mutliple charts:
Sheets("Summary").ChartObj ects(3).Se lect
With Selection
With ActiveChart.SeriesCollecti on(1).Tren dlines(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?
Sheets("Summary").ChartObj
With Selection
With ActiveChart.SeriesCollecti
.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).Trendl ines(1)
.DisplayRSquared = False
.DisplayEquation = True
.DataLabel.Left = 25
.DataLabel.Top = 0
End With
Case Else
End Select
Next cht
End Sub
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
.DisplayRSquared = False
.DisplayEquation = True
.DataLabel.Left = 25
.DataLabel.Top = 0
End With
Case Else
End Select
Next cht
End Sub
ASKER
.DataLabel.Left = cht.Width - 65
and
.DataLabel.Left = cht.Width + 65
But neither worked. With the equation on the right, it overlaps my title.