Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建从Excel VBA到PowerPoint的图表,并将源链接到Excel中的特定表_Excel_Vba_Powerpoint 2010 - Fatal编程技术网

创建从Excel VBA到PowerPoint的图表,并将源链接到Excel中的特定表

创建从Excel VBA到PowerPoint的图表,并将源链接到Excel中的特定表,excel,vba,powerpoint-2010,Excel,Vba,Powerpoint 2010,我想: 从VBA Excel在PowerPoint中创建图表 将数据源链接到Excel文件中的范围 或者将PowerPoint存储数据源的工作表替换为“我的数据” 我是VBA的初学者(尝试学习一周) 我这样做: Dim ChartDataRange As Excel.Range Dim myWorksheet As Worksheet Set ChartDataRange = Excel.Range("Chart_Data_Range") Dim ChartRelativePerfBox As

我想:

从VBA Excel在PowerPoint中创建图表 将数据源链接到Excel文件中的范围 或者将PowerPoint存储数据源的工作表替换为“我的数据”

我是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的链接对象

您可以在工作簿中创建图形,然后通过执行以下操作复制图形并将其粘贴到powerpoint中:

  • Powerpoint->主页功能区->剪贴板
  • 单击下拉箭头->粘贴特殊
  • 单击“粘贴链接”
  • 单击“确定”
  • 现在,您将有一个图表链接到工作簿中的图表。每当您手动刷新图表(右键单击->更新链接)或打开powerpoint演示文稿时,此选项都会更新

    文档可在上找到