这是我将使用的方法:
- 最初使用插入图表在 PPT 中设置图表。
- 然后从 VBA 中,为每个图表从 Excel 源文件中收集数据并将数据存储在
array
变量中。
- 使用这些变量来更新图表的系列数据(或者更新 powerpoint 图表的嵌入式工作表
.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
此方法不会写入图表的数据表。坦率地说,如果您正在创建一个宏驱动的仪表板,我认为这是一个不必要的步骤,应该没有任何理由需要数据表,但如果出于某种原因需要,我们可以修改图表系列的创建方式。