这是方法,我会用:
- 使用插入图表初步建立起排行榜PPT。
- 然后从VBA,为每个图表收集来自源的Excel文件 数据并且将数据存储在
array
变量。
- 使用这些变量来更新图表的系列数据(或者更新的PowerPoint图表的嵌入的工作表
.ChartData
)。
还有其他的方法,比如使用OLEObjects链接/嵌入,但坦率地说这些都是与工作一痛,如果文件(S)是一个共享驱动器上,如果他们移动或会引起问题改名等
这是我上面描述的一般框架。
这将需要在您的结尾进行大量的修改 - 例如,仅为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
此方法不不写入到图表的数据表。坦率地说,如果你创建一个宏驱动的仪表板,那么作为一个不必要的步骤,应该没有任何理由需要数据表,但如果出于某种原因需要这样做,我们可以修改图表系列的创建方式。
通过“简单的解决方案”你的意思做,还是其他什么东西的手动/非自动化的方式? – 2013-04-26 14:53:23
通过一个简单的解决方案,我的意思是一个微软向导,显然不存在。我刚刚阅读了几个问题,有些人提出了这样一个“简单的解决方案”,我的意思是反对这样的建议,在我的问题中加入这一行。 – John 2013-04-29 07:48:01