2013-04-26 28 views
0

我一直在试图找到一种解决方案,无论是宏观还是简单的解决方案,都可以在PowerPoint演示文稿中创建和格式化图表。到目前为止,我找不到任何能解决我的问题的东西。 这个想法是从一个相当大的Excel文件中获取数据,然后在几个幻灯片上创建几个图表。也就是说,一个大的excel文件和10个幻灯片幻灯片,每张幻灯片上有8个独立的图表。 我试过这个:http://mahipalreddy.com/vba.htm#ppgraph,但这根本没有帮助。如何基于Excel数据在PowerPoint中创建和格式化各种图表?

我该如何解决这个问题?

+0

通过“简单的解决方案”你的意思做,还是其他什么东西的手动/非自动化的方式? – 2013-04-26 14:53:23

+0

通过一个简单的解决方案,我的意思是一个微软向导,显然不存在。我刚刚阅读了几个问题,有些人提出了这样一个“简单的解决方案”,我的意思是反对这样的建议,在我的问题中加入这一行。 – John 2013-04-29 07:48:01

回答

1

这是方法,我会用:

  1. 使用插入图表初步建立起排行榜PPT。
  2. 然后从VBA,为每个图表收集来自源的Excel文件 数据并且将数据存储在array变量。
  3. 使用这些变量来更新图表的系列数据(或者更新的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 

此方法不写入到图表的数据表。坦率地说,如果你创建一个宏驱动的仪表板,那么作为一个不必要的步骤,应该没有任何理由需要数据表,但如果出于某种原因需要这样做,我们可以修改图表系列的创建方式。

+0

哇!非常感谢你......我将在周五的办公室里看看我是否可以实现你的代码。再次感谢您花时间回答我的问题。 – John 2013-04-29 07:48:59

+1

将它捆绑到一个专门打包的xl文件将导致相同的问题,链接的文件将取代wbFileName =“C:\ users \ david_zemens \ desktop \ dummy chart data.xlsx”它可能会更好wbFileName = ActivePresentation.Path& “\”&“dummy chart data.xlsx”这样,只要XLSX与PPTX位于同一个文件夹中,无论在哪里,它都可以工作。 – 2013-04-29 15:12:58

+0

@SteveRindsberg,这是一个有效的点。我上面发布的方法可以很容易地为此目的进行修改,或者使用'Application.GetOpenFileName'方法从任何位置检索文件。我比OLEObjects/Linked文件更喜欢这种方法 - 当然你需要知道源数据XLS文件的位置以执行宏。 – 2013-04-29 15:19:08

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

+0

感谢您发布此信息,但是在工作中,并非所有人都能够,也不会允许您安装外部插件。 – John 2013-05-03 07:35:05

相关问题