How to create and format various diagrams in powerpoint based on excel data?

I'm trying to find a solution, whether itโ€™s a macro or a simple solution, to create and format charts in a PowerPoint presentation. So far, I could not find anything that could solve my problem. The idea is to transfer the source data from a rather large excel file, and then create multiple diagrams on multiple PowerPoint slides. That is, one large excel file and 10 Powerpoint slides with 8 separate diagrams on each slide. I tried this: http://mahipalreddy.com/vba.htm#ppgraph , but that didn't help at all.

How can i solve this?

0
source share
2 answers

This is the approach I would use:

  • Set up charts natively in PPT using the Insert Chart.
  • Then, from VBA, for each diagram, data is collected from the Excel source file and the data is stored in array variables.
  • Use these variables to update the data in a series of charts (alternatively, update the built-in chart sheet Powerpoint .ChartData ).

There are other methods, such as using OLEObjects for linking / embedding, but frankly, this is a pain for work and can cause problems if files (files) are on a shared drive, if they are moved or renamed, etc.

Here is the general structure described above.

This will require a lot of modification at your end - for example, it is configured for only 1 chart on 1 slide, and I have no idea how your data is organized in Excel, so I just insert some kind of dummy code to show how I captured some values from Excel, you obviously will need to fine-tune it with a good amount of code so that it is dynamic enough to work in all diagrams (this can be done quite easily if your data is well organized and you know your way around Excel VBA).

 Option Explicit Option Base 1 Sub GetChartDataFromXLS() Dim wbFileName As String '## full filename & path of the Excel file.' Dim oXL As Object Dim xlWB As Object Dim xlWS As Object Dim cl As Object Dim c As Long Dim shp As Shape Dim cht As Chart Dim srs As Series Dim x As Long Dim sArray() As Variant '## temporary array for each series, will be stored in chtData array.' Dim chtData() As Variant '## I would use this array to store several arrays from the Excel file.' Dim s As Long wbFileName = "C:\users\david_zemens\desktop\dummy chart data.xlsx" Set oXL = CreateObject("Excel.Application") oXL.Visible = True Set xlWB = oXL.Workbooks.Open(wbFileName) '## iterate over the shapes in the slide.' For Each shp In ActivePresentation.Windows(1).Selection.SlideRange(1).Shapes '## check to see if this shape is a chart.' If shp.HasChart Then '## set the chart variable.' Set cht = shp.Chart '## clear out any existing series data in the chart' For s = cht.SeriesCollection.Count To 1 Step -1 Set srs = cht.SeriesCollection(s) srs.Delete Next '##Your code to get the chtData will go in this block:' '## Set xlWS = xlWB.Sheets(1) ' ##Modify to get the correct sheet where the data for this chart resides' '## It will probably be something like this, which ' ' iterates over some columns and collects data in to a series' ' of arrays, stored within chtData array ' For x = 1 To 3 'However Many Series you need to add:' 'Assuming data series begins in column A, etc...' c = 1 For Each cl In xlWS.Range("A1:A10").Offset(0, x - 1) ReDim Preserve sArray(c) sArray(c) = cl.Value c = c + 1 Next 'ReDim Preserve the chtData array ReDim Preserve chtData(x) chtData(x) = sArray Next x '## End collection of the chart data. '## Expose the data sheet but minimize it to preserve updating cht.ChartData.Activate cht.ChartData.Workbook.Application.WindowState = -4140 '## Now, take that data and insert it to the chart If LBound(chtData) >= 1 Then For s = LBound(chtData) To UBound(chtData) '## Add a new series to the chart Set srs = cht.SeriesCollection.NewSeries srs.Values = chtData(s) '## Modify this line to point at the appropriate array from chtData' 'manipulate the other series properties here ' 'srs.Name = "whatever the series name" ' 'srs.XValues = "whatever the series value" ' '# etc... '# etc... Next 'Next series... End If '## Close the chartdata sheet. cht.ChartData.Workbook.Close End If Next oXL.ActiveWorkbook.Close oXL.Quit On Error Resume Next Set oXL = Nothing Set xlWB = Nothing Set xlWS = Nothing On Error GoTo 0 End Sub 

This method does not write to the chart data sheet. Honestly, I see that as an unnecessary step, if you are creating a control panel with macro definition, there should be no reason to need a data sheet, but if this is necessary for some reason, we can change the way we create a chart series.

+1
source

Another method would be to use a plugin for PowerPoint plugins called oomfo @ http://oomfo.com

Using oomfo, you can create charts that are connected to Excel worksheets. After you have built the chart that is associated with the Excel data source, whenever the Excel worksheet is updated and the presentation is viewed, the charts automatically retrieve the latest data. You just need to make sure that PowerPoint must have access to this Excel file (locally or remotely).

The link to the Excel data source documentation is at http://docs.oomfo.com/charts/1.0/contents/chart_data/data_excel.html

0
source

Source: https://habr.com/ru/post/946132/


All Articles