我一直试图找到一个解决方案,无论是宏还是简单的解决方案,在powerpoint演示文稿中创建和格式化图表。到目前为止,我找不到任何可以解决我的问题的东西。 我们的想法是从一个相当大的excel文件中获取数据,然后在几个powerpoint幻灯片上创建几个图表。也就是说,一个大的excel文件和10个powerpoint幻灯片,每张幻灯片上有8个单独的图表。 我试过这个:http://mahipalreddy.com/vba.htm#ppgraph,但这根本没有帮助。
我该如何解决这个问题?
答案 0 :(得分:1)
这是我将使用的方法:
array
变量中并存储。 .ChartData
)。还有其他一些方法,比如使用OLEObjects来链接/嵌入,但坦率地说,这些方法很难处理,如果文件位于共享驱动器上,如果它们被移动或重命名等,则可能会出现问题。
以下是我在上面描述的一般框架。
这将需要你的大量修改 - 例如,这只是为1张幻灯片上的1个图表配置,我不知道你的Excel中的数据是如何排列的,所以我只是输入一些虚拟代码为了展示我如何从Excel中捕获一些值,您显然需要使用大量代码对其进行微调,以使其足够动态以适用于所有图表(如果您的数据组织良好,这可以很容易地完成,你了解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
此方法不写入图表的数据表。坦率地说,如果您要创建一个宏驱动的仪表板,我认为这是一个不必要的步骤,不应该有任何理由需要数据表,但如果由于某种原因需要,我们可以修改图表系列的创建方式。
答案 1 :(得分:0)
另一种方法是使用免费的PowerPoint图表插件oomfo @ http://oomfo.com
使用oomfo,您可以构建连接到实时Excel工作表的图表。一旦构建了连接到Excel数据源的图表,每当Excel工作表更新并查看演示文稿时,图表会自动提取最新数据。您只需要确保PowerPoint可以访问该Excel文件(本地或远程)。
Excel数据源文档的链接位于http://docs.oomfo.com/charts/1.0/contents/chart_data/data_excel.html