从Excel VBA创build图表到PowerPoint,并将源代码链接到Excel中的特定表格

我想要:

在PowerPoint中创build一个图表从Excel中将数据源链接到我在Excel文件中的范围或者将PowerPoint存储数据源的工作表replace为我的数据

我是VBA的初学者(一周试图学习)

我这样做:

Dim ChartDataRange As Excel.Range Dim myWorksheet As Worksheet Set ChartDataRange = Excel.Range("Chart_Data_Range") Dim ChartRelativePerfBox As PowerPoint.Shape 'Now open PowerPoint Dim pptApp As New PowerPoint.Application 'Keep it visible because we need to see it and be able to save it pptApp.Visible = True 'Tell VBA that FinancialSlide is a PowerPoint presentation Dim FinancialSlide As PowerPoint.Presentation 'Add new presentation Set FinancialSlide = pptApp.Presentations.Add 'Tell VBA that firstslide is a PowerPoint slide Dim Slide1 As PowerPoint.Slide 'Create the first slide Set Slide1 = FinancialSlide.Slides.Add(1,PowerPoint.PpSlideLayout.ppLayoutBlank) Set ChartRelativePerfBox = Slide1.Shapes.AddChart( _ Type:=xlLine, _ Left:=Application.CentimetersToPoints(12.95), _ Top:=Application.CentimetersToPoints(12.6), _ Width:=Application.CentimetersToPoints(11.45), _ Height:=Application.CentimetersToPoints(5.5)) Set myWorksheet = ChartRelativePerfBox.Chart.ChartData.Workbook.Worksheet(1) 'Replace data source in the worksheet by desired data Application.DisplayAlerts = False 'Copy desired data ChartDataRange.Copy 'Paste them in the chart worksheet myWorksheet.Range("A1").PasteSpecial (xlPasteValuesAndNumberFormats) 'Delete extra columns in worksheet myWorksheet.Columns("D:E").Select Selection.Delete 'Resize to used range myWorksheet.ListObjects("Table1").Resize myWorksheet.UsedRange Application.DisplayAlerts = True 

我试着用:

 Set ChartRelativePerfBox.SetSourceData( _ Source:= ChartDataRange, _ PlotBy:= xlLine) 

但它不起作用。 任何猜测?

我以前的代码工作,但它是如此缓慢..

作为一个在VBA中这样做的解决方法,你有没有想过使用excel链接的对象到powerpoint?

您可以在工作簿中创buildgraphics,然后通过执行以下操作复制graphics并将其粘贴到PowerPoint中:

  1. Powerpoint – >家庭function区 – >剪贴板。
  2. 点击下拉箭头 – >粘贴特殊。
  3. 点击“粘贴链接”。
  4. 点击OK。

现在,您将有一个图表链接到工作簿中的图表。 无论何时手动刷新图表(右键单击 – >更新链接)或打开PowerPoint演示文稿,都会更新。

文件可以在办公室网站上find。