Solved

Excel 2007 VBA: Applying a gradient to a Series Collection Trendline

Posted on 2013-06-03
6
818 Views
Last Modified: 2013-06-07
How do I modify this code to produce a gradient in the trendline?

Sub HideShowTrendline()
Dim trig As Range
Set trig = ActiveSheet.Shapes("Chart 1").TopLeftCell.Offset(2, 2)
If trig = 1 Then
   ActiveSheet.ChartObjects("Chart 1").Activate
   ActiveChart.SeriesCollection(5).Trendlines(1).Delete
   trig = ""
Else
   ActiveSheet.ChartObjects("Chart 1").Activate
   ActiveChart.SeriesCollection(5).Select
   ActiveChart.SeriesCollection(5).Trendlines.Add
   ActiveChart.SeriesCollection(5).Trendlines(1).Select
      With ActiveChart.SeriesCollection(5).Trendlines(1)
         .Border.Weight = xlThick
         .Border.LineStyle = xlContinuous
         .Border.ColorIndex = 17
         .Border.Weight = xlThick
         .Border.LineStyle = xlContinuous
         .Format.Shadow.Type = msoShadow22
      End With
   trig = 1
End If
[A4].Select
End Sub

Open in new window

Thanks,
John
0
Comment
Question by:gabrielPennyback
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39217356
What do you mean?  Do you want to display the gradient on the trendline?  You can display a formula which might do what you ask, but maybe you want to change the gradient? Please be specific.
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 39217514
Change the "fill" of the trendline to a gradient. It's too bad they removed the ability to record shape formats in 2007, or I wouldn't even need to ask this question! :- )
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39218222
Sorry - I automatically thought of the line gradient in the context of a chart!  I'm sorry I can't work out how to do this.  You can get the trendline, and I would expect to be able to do something like this:
    Dim t As Trendline
    
    Set t = ActiveChart.SeriesCollection(1).Trendlines(1)
    
    With t.Format.Fill
        .TwoColorGradient msoGradientFromCenter, 2
    End With

Open in new window

but the Format.Fill does not work on a trendline, and I can't work out an alternative.  Maybe you just can't do it except through the user interface - it never records, even in Excel 2013.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:gabrielPennyback
ID: 39219903
Thanks, andrewssd3. I'll leave this question  open for a while just in case someone knows how to do it. I guess it's not the end of the world if I can't accomplish it!
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 39220318
One way that does work is to create a custom chart template from an existing chart, then apply it to a new one.  So if you create a chart with a trendline, format the gradient on the trendline as you want, then right click on the chart and select Save as template...  Then for future charts you can start them then use VBA code similar to this to apply the chart template:
    ActiveChart.ApplyChartTemplate ( _
        "C:\Users\youruser\AppData\Roaming\Microsoft\Templates\Charts\gradient.crtx")

Open in new window

BTW this is a powerful way of applying a set of standard formats to charts. The templates store colours and formats etc and allows you to get a coherent style set quickly and easily.
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 39230126
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now