I have an inserted chart in powerpoint. I am using this as a template. I would want to edit the data of this chart with data from an excel sheet. Is there an excel vba code for this
-
I have a power point slide template which has a inserted bar chart. I have to create 10 slides with different data using this temlate. So each chart in the 10 slides will have different data. Now, this data for updating these charts exists in excel. Is there any VBA code where the data of inserted chart in the ppt can be updated with the data in the excel – Manoj Kumar Oct 10 '16 at 20:08
-
1) Revisions & clarifications should be reflected by [editing your question](http://stackoverflow.com/posts/39965337/edit) and 2) that comment still doesn't demonstrate any effort on your part. Please show what you have tried so far (code) and indicate your *actual* problem. – David Zemens Oct 10 '16 at 20:09
-
*Is there any VBA code where the data of inserted chart in the ppt can be updated with the data in the excel?* Yes, but you will need to create the VBA code to do this, based on your actual use-case & needs. – David Zemens Oct 10 '16 at 20:11
-
Hi David. I do not know where to start as I have searched for sample code and got nowhere. Just assume there is one chart in a presentation and we need to update the data of the chart with data from an external excel file – Manoj Kumar Oct 10 '16 at 20:16
-
Are you trying to embed & **link** the PowerPoint chart to the Excel data? Or are you simply trying (it is not so simple, really) update the data in PowerPoint from the data in Excel? – David Zemens Oct 10 '16 at 20:20
-
No. I am not trying to embed and link. What I am trying to automate is: For example if we have a chart in powerpoint slide. First I create a copy of the slide. If we right click and click edit on the chart an excel sheet opens. It has some dummy data in it. Now, I will update the data of the chart to my preference. I would like to automate this. I have created the copy of the slide using vba code. Now I have to change the data of the chart in the ppt by assigning a range from an external excel. Is it possible?? – Manoj Kumar Oct 10 '16 at 20:32
-
Has my answer been of an help to you? – David Zemens Oct 11 '16 at 13:39
-
Hi David. Sorry for the delayed response. Did not work on it after that. Will try and get back. Thanks again!! – Manoj Kumar Oct 12 '16 at 10:52
-
Hi Davis. Thanks for the input. It worked. Now I am able to edit the existing chart in the powerpoint with data in the excel sheet – Manoj Kumar Oct 13 '16 at 08:51
3 Answers
Any Chart in PowerPoint (there are exceptions, and it is possible to "break" existing charts, but that's not in scope, here) has a ChartData
property, which returns an Excel Workbook that contains the data for the chart.
When working from a "template" slide, it should be safe to assume that the chart data exists on Sheet 1, and in a ListObject
table (there should be only one such table in the sheet).
In PowerPoint VBA, requiring reference to Excel object library, this shows you how to get a handle on the ListObject
which contains the chart's data:
Sub ShowChartData()
Dim sld As Slide
Dim shp As Shape
Dim cht As Chart
Dim chtData As ChartData
Dim cTable As Excel.ListObject
'Assume we have only one slide, at slide 1:
Set sld = ActivePresentation.Slides(1)
'Assume the Chart is the second shape, modify if needed
Set shp = sld.Shapes(2)
'Handle the chart
Set cht = shp.Chart
'Handle the CharttData
Set chtData = cht.ChartData
'Open & minimize the ChartData, you don't need to see it, but it must be OPEN to edit it
chtData.Activate
chtData.Workbook.Application.WindowState = -4140
With chtData
Set cTable = chtData.Workbook.Worksheets(1).ListObjects(1)
' Here, you can update the ListObject in the same ways you
' would do so in Excel, natively.
End With
'Remember to close the workbook
chtData.Workbook.Close
End Sub
Now that you have a handle on the ListObject
, you need to somehow get the values from Excel.
This will require handling an open instance of Excel.Application
class (or prompting the user to select a file from a FileDialog, etc.) and identifying which data to put in the PowerPoint, and how to arrange it. Usually this can be done by dumping the values from Excel in to a variant array, and passing that to PowerPoint.
Since those are all details you've omitted, please note that I am absolutely not willing to entertain what is likely to be a never-ending series of "but how do I do such-and-such...?" follow-up questions as you suss out the complexities of your own logic and use-case requirements.
The above code is designed to execute from PowerPoint. If you need to run it from Excel, it will require different code (untested, but something like this).
Sub ShowPPTChartData()
' to be run from Excel VBA
'Requires reference to PowerPoint library
Dim ppt as PowerPoint.Application
Dim pres as PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim cht As PowerPoint.Chart
Dim chtData As PowerPoint.ChartData
Dim cTable As Excel.ListObject
Set ppt = GetObject(,"PowerPoint.Application")
'Assume we have only one open Presentation file:
Set pres = ppt.Presentations(1)
'Assume we have only one slide, at slide 1:
Set sld = pres.Slides(1)
'Assume the Chart is the second shape, modify if needed
Set shp = sld.Shapes(2)
'Handle the chart
Set cht = shp.Chart
'Handle the CharttData
Set chtData = cht.ChartData
'Open & minimize the ChartData, you don't need to see it, but it must be OPEN to edit it
chtData.Activate
chtData.Workbook.Application.WindowState = -4140
With chtData
Set cTable = chtData.Workbook.Worksheets(1).ListObjects(1)
' Here, you can update the ListObject in the same ways you
' would do so in Excel, natively.
End With
'Remember to close the workbook
chtData.Workbook.Close
End Sub
EDIT It is possible to edit an existing chart without Activating the ChartData.Workbook
as can be demonstrated here:
Update PowerPoint chart without opening chart workbook or making it invisible
Adding/removing series from the charts is trickier than manipulating data that's already part of the chart series, however.

- 53,033
- 11
- 81
- 130
-
-
I wrote that in yesterday, and never pressed send. Strange... But anyways, I wanted to "revive" the thread because this is the only post I found that seems to answer any basic questions about how to operate on PowerPoint charts. I've been using the above method (`chtData.Activate`) for a while now, but it's extremely slow and buggy when having to edit multiple charts. I just wanted to ask you if you if by any chance you've figured out a better/faster way to work with those charts since this post? In my experience adding `chtData.Workbook.Application.WindowState = -4140` only slows things down. – Pinlop Oct 19 '17 at 13:18
-
1@Pinlop I'm sorry but no, there is unfortunately no workaround for the `ChartData.Activate` -- see [here](https://stackoverflow.com/questions/43592870/run-an-excel-macro-to-update-a-powerpoint-linked-chart-workbook-already-open/43594610#43594610) for explanation. – David Zemens Oct 19 '17 at 13:25
-
Thank you kindly for the response. Now I can actually run my code without having that queasy feeling in my stomach that I'm learning/doing something wrong (Still doesn't make the design any better haha.) Sorry, but one more question for you: would you happen to have a link to a post that explains the importance of using `ListObjects`? I just treat the "Chart in Microsoft PowerPoint" Sheet as a regular Excel sheet, and use things like `Worksheets(1).UsedRange.Clear` and `SetSourceData` to erase old data and tell PPT where new data is, I've never used ListObjects and don't understand the need. – Pinlop Oct 19 '17 at 14:16
-
1@Pinlop It *is* a regular Excel sheet, but depending on how the chart was created, data may exist in a `ListObject` (*Table* object) in Excel. This happens if you insert a default chart via PowerPoint ribbon, but if you importing charts that were created manually in Excel, then whether the data is a `ListObject` or a simple `Range` depends on how and who created the table. There's no strict *need* to use `ListObject`, but if that's how the data is already organized, you should, because it has handy methods like `HeaderRowRange` and `DataBodyRange` and `ListRows` and `ListColumns`, etc. – David Zemens Oct 19 '17 at 14:21
-
Ah I get it now. It's really hard to find people who understand PowerPoint VBA to ask questions to, and I'm just learning. So thank you, I really appreciate the help! – Pinlop Oct 19 '17 at 14:39
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157094/discussion-between-pinlop-and-david-zemens). – Pinlop Oct 19 '17 at 18:44
Set Current_Chart_Shape = Active_Slide.Shapes("Monthly Chart")
Set Current_Chart = Current_Chart_Shape.Chart
Set Current_Chart_Data = Current_Chart.ChartData
Current_Chart_Data.Activate
Set PPTChartSheet = Current_Chart.ChartData.Workbook.Sheets(1)
Current_Chart_Data.Workbook.Application.WindowState = -4140
With PPTChartSheet
.ListObjects("Table1").Resize PPTChartSheet.Range("A1:C8")
For l = 0 To 6
.Range("B2").Offset(l, 0).Value = Array_Values(l)
Next l
End With
Current_Chart_Data.Workbook.Close

- 31
- 1
- 4
-
The .Resize will resize the chart data range and after that we can add value directly or through an array as David suggested. Sorry for not being clear before. Thanks for your patience!! – Manoj Kumar Oct 13 '16 at 09:01
You don't need to do anything special.
Once a chart is embedded and linked to an Excel sheet, PowerPoint couldn't care less when or how it's updated, even when the PowerPoint file is closed.
Basically, whatever you do to that sheet, VB or manually, will be reflected in the PowerPoint next time you open it.

- 4,666
- 2
- 16
- 30
-
Sorry, I think I asked the wrong question. The real question is: I have an inserted chart in powerpoint. I am using this as a template. I would want to edit the data of this chart with data from an excel sheet. – Manoj Kumar Oct 10 '16 at 19:43
-
Oh, well you are doing it backwards. You need to create the chart in Excel. Then paste that chart into PowerPoint. From that point forward, whenever you right/click on the chart to Edit Data, it will pull up the Excel sheet. And, the reverse... When you edit the chart, it will be updated in PowerPoint. – durbnpoisn Oct 10 '16 at 19:51
-
So, is there no way using powerpoint chart objects to update the data in the chart ?? – Manoj Kumar Oct 10 '16 at 19:56
-
Well... Yes. You can... You right click on the chart and say you want to edit the data in Excel. Once you do that, you save the file. It will then be the linked data source. – durbnpoisn Oct 10 '16 at 20:00
-
You want to use VBA to create a chart and link it to an external Excel file?! – durbnpoisn Oct 10 '16 at 20:10
-
-
Ok, again... Right click the chart, edit in Excel. Then your link is made. Once that's done, you can use any means - including VBA - to update your chart. – durbnpoisn Oct 10 '16 at 20:23
-
@durbnpoisn this doesn't really answer the question. OP doesn't seem to want to link/embedded Excel file, and simply offering that "you can use any means - including VBA" doesn't really answer the question which pertains to "how to do this using VBA" (in fairness to you, it's a low quality question). – David Zemens Oct 10 '16 at 20:34