Link to home
Start Free TrialLog in
Avatar of Gemstorm
Gemstorm

asked on

Apply Colour Changes to Excel Chart in PowerPoint using VBA

Hi,
Through VBA how do I change the colours of the data series of a chart that's on a Powerpoint slide or alternatively can I apply a .CRTX template to an existing chart - again through VBA.

Many Thanks.
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

ActiveSheet.ChartObjects("Chart1").Activate
ActiveChart.SeriesCollection(3).Select
ActiveChart.SeriesCollection(3).Interior.Color = RGB(255, 0, 0)

Avatar of Gemstorm
Gemstorm

ASKER

Sorry maybe I wasn't clear enough - I'm working in PowerPoint - your solution uses ActiveSheet to reference the chart. I won't be working in Excel so I need to reference the chart through PowerPoint.
for example you can use following example in powerpoint
Dim oGraph As Graph.Chart
Set oGraph = oPPTShape.OLEFormat.Object
oGraph.SeriesCollection(1).Interior.ColorIndex = 3
Hi ukerandi,
thanks for the suggestion. However I'm getting the following error

Dim oGraph As Graph.Chart: Compile error - user defined type not defined
check my code
before run my programme
Goto Developer and Visual basic menue,click on it and add module and paste my code below

On the Tools menu, click References.

In the References dialog box, scroll until you see Microsoft Excel 14.0 Object Library,
Microsoft Powerpoint 12.0 Object Library and  Microsoft Office 12.0
select it, and then click OK.

Close the VBE.
then goto Developer menue and click on visual basic menue -. then click on to run
Sub CreateChart()

Dim myChart As Chart
   Dim gChartData As ChartData
    Dim gWorkBook As Excel.Workbook
  Dim gWorkSheet As Excel.Worksheet      ' Create the chart and set a reference to the chart data.
   Set myChart = ActivePresentation.Slides(1).Shapes.AddChart.Chart
  Set gChartData = myChart.ChartData
' Set the Workbook and Worksheet references.
 Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)      ' Add the data to the workbook.
  gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:B5")
  gWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
  gWorkSheet.Range("A2").Value = "Coffee"
 gWorkSheet.Range("A3").Value = "Soda"
 gWorkSheet.Range("A4").Value = "Tea"
  gWorkSheet.Range("A5").Value = "Water"
   gWorkSheet.Range("B2").Value = "1000"
 gWorkSheet.Range("B3").Value = "2500"
 gWorkSheet.Range("B4").Value = "4000"
   gWorkSheet.Range("B5").Value = "3000"
   ' Apply styles to the chart.
   With myChart
   .ChartStyle = 4
 .ApplyLayout 4
.ClearToMatchStyle

  End With      ' Add the axis title.
  With myChart.Axes(xlValue)
 .HasTitle = True
  .AxisTitle.Text = "Units"
 End With      'myChart.ApplyDataLabels
  ' Clean up the references.
 Set gWorkSheet = Nothing
 ' gWorkBook.Application.Quit
   Set gWorkBook = Nothing
 Set gChartData = Nothing
Set myChart = Nothing
 End Sub

Open in new window

For your Error  "oGraph As Graph.Chart: Compile error - user defined type not defined "
please follow this instructions

On the Tools menu, point to Macros, and then click Visual Basic Editor.
On the Tools menu, click References.
In the "References - VBAProject" dialog box, click to select the "Microsoft Graph 8.0 Object Library" check box.
Click OK.
ASKER CERTIFIED SOLUTION
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland 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
I've requested that this question be closed as follows:

Accepted answer: 500 points for ukerandi's comment http:/Q_27238476.html#36367997

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Apologies for not closing this question. I have closed the question properly.
Everything suggested above will work with PowerPoint 2003 as it references charts through the Chart Object. However PowerPoint 2007 and 2010 use the Excel Chart object - despite exhaustive searching there doesn't appear to be any way to manipluate a chart idirectly in PowerPoint 2007/2010.