+ Reply to Thread
Results 1 to 6 of 6

Thread: trendline position using vba

  1. #1
    Registered User
    Join Date
    11-05-2011
    Location
    Indpls, IN
    MS-Off Ver
    Excel 2010
    Posts
    5

    trendline position using vba

    W7, ultimate
    Office 10

    I am creating hundreds of charts using modified code from the following thread:

    http://www.excelforum.com/excel-char...le-charts.html

    I've added a trendline and displayed equation to the chart (xlscatter): however, the trendline equation often ends up overlapping the data and trendline. I tried using a select and move with mixed results since it is a relative move. Is there any way to do a more precise move relative to 0,0 or some ordinal position on the chart?

    code I am using to move trendline equation:

    ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
        Selection.Left = 95.77
        Selection.Top = 91.873
    The problem being that the Selection.Left or Selection.Top is relative to the current position of the equation datalable.

    I also assume the units are in TWIPS? At least for graphs it used to be.
    Last edited by wejones; 11-11-2011 at 03:48 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: trendline position using vba

    Were should it be positioned?
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    11-05-2011
    Location
    Indpls, IN
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: trendline position using vba

    Since this is just a single plot of x,y data any open area is ok. The most consistent open area is off of the y axis and I was going to put it up about 1/2 way. I've just started working on taking the negative of the position using the position property, then the difference between some fixed location and the label position to get the amount to move the label. I'm not sure how to differentiate needing to move the label up or down yet.

  4. #4
    Registered User
    Join Date
    11-05-2011
    Location
    Indpls, IN
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: trendline position using vba

    I think the following code is a start to a solution:

     .SeriesCollection(1).Trendlines(1).DataLabel.Select
            'trend label position relative to chart edges
            moveleft = .SeriesCollection(1).Trendlines(1).DataLabel.Left
            movetop = .SeriesCollection(1).Trendlines(1).DataLabel.Top
            'shift position relative to current position
            Selection.Left = moveleft - 10
            Selection.Top = movetop - 5
    Last edited by wejones; 11-11-2011 at 03:32 PM.

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: trendline position using vba

    this will position top right of plot area.

    activechart.SeriesCollection(1).trendlines(1).datalabel.left = activechart.PlotArea.Left+activechart.PlotArea.width
    activechart.SeriesCollection(1).trendlines(1).datalabel.top= activechart.PlotArea.top
    There must be enough space between the edge of the plot area and the edge of the chartarea other wise the data label in overlay the plotarea.
    Cheers
    Andy
    www.andypope.info

  6. #6
    Registered User
    Join Date
    11-05-2011
    Location
    Indpls, IN
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: trendline position using vba

    Andy:

    Works well also. After trying both approaches the relative offset from the default position gave me the best number of charts that did not require additional formatting.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0