+ Reply to Thread
Results 1 to 6 of 6

Adding trendlines to a chart while using a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    07-25-2009
    Location
    Canyon, Texas
    MS-Off Ver
    Excel 2007
    Posts
    4

    Adding trendlines to a chart while using a macro

    I keep having the same problem occur no matter what I try. When using a macro to provide a chart from a given data set, the trendline that I am trying to use along with the chart will not work and keeps giving a runtime error. Any hints???

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Adding trendlines to a chart while using a macro

    Welcome to the forum.

    Post the workbook with your code.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-25-2009
    Location
    Canyon, Texas
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Adding trendlines to a chart while using a macro

    This is what im trying to chart with a exponential trendline using a macro. Its a simple task but I just can't figure out what I'm missing. This is the data that I'm using.


    Year Sales
    1 70
    2 183
    3 340
    4 649
    5 1243
    6 1979
    7 4096
    8 6440
    9 8459
    10 12154



    Sub ExpTrendChart()
    '
    ' ExpTrendChart Macro
    '

    '
    Range("A1:B11").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("'12.19'!$A$1:$B$11")
    ActiveChart.ChartType = xlXYScatter
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Trendlines.Add
    ActiveSheet.ChartObjects("Chart 11").Activate
    ActiveChart.SeriesCollection(1).Trendlines(1).Select
    Selection.Type = xlExponential
    End Sub

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Adding trendlines to a chart while using a macro

    Please take a few minutes to read the forum rules, and then edit you post to add code tags.

  5. #5
    Registered User
    Join Date
    07-25-2009
    Location
    Canyon, Texas
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Adding trendlines to a chart while using a macro

    This is what im trying to chart with a exponential trendline using a macro. Its a simple task but I just can't figure out what I'm missing. This is the data that I'm using.


    
    Year	Sales
    1	70
    2	183
    3	340
    4	649
    5	1243
    6	1979
    7	4096
    8	6440
    9	8459
    10	12154
    
    Sub ExpTrendChart()
    '
    ' ExpTrendChart Macro
    '
    
    '
        Range("A1:B11").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Range("'12.19'!$A$1:$B$11")
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SeriesCollection(1).Select
        ActiveChart.SeriesCollection(1).Trendlines.Add
        ActiveSheet.ChartObjects("Chart 11").Activate
        ActiveChart.SeriesCollection(1).Trendlines(1).Select
        Selection.Type = xlExponential
    End Sub

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Adding trendlines to a chart while using a macro

    If you remove the code that specifies a chart to activate then it works,
    That line would have been correct once but subsequent runs of the macro would mean the chart name changes.

        ActiveSheet.ChartObjects("Chart 11").Activate

    Sub ExpTrendChart()
    '
    ' ExpTrendChart Macro
    '
    
    '
        Range("A1:B11").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Range("'12.19'!$A$1:$B$11")
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SeriesCollection(1).Select
        ActiveChart.SeriesCollection(1).Trendlines.Add
        ActiveChart.SeriesCollection(1).Trendlines(1).Select
        Selection.Type = xlExponential
    End Sub
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1