【问题标题】:Create a chart from Excel VBA to PowerPoint and link the source to a specific table in Excel创建从 Excel VBA 到 PowerPoint 的图表,并将源链接到 Excel 中的特定表格
【发布时间】:2015-07-08 07:46:23
【问题描述】:

我想:

从 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)

但它不起作用。有什么猜测吗?

我之前的代码可以运行,但是速度太慢了..

【问题讨论】:

    标签: excel vba powerpoint-2010


    【解决方案1】:

    作为在 VBA 中执行此操作的一种解决方法,您是否考虑过使用从 excel 到 powerpoint 的链接对象?

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

    1. Powerpoint -> 主页功能区 -> 剪贴板。
    2. 点击下拉箭头 -> 选择性粘贴。
    3. 点击“粘贴链接”。
    4. 点击确定。

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

    可以在office website 上找到文档。

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2014-02-25
      相关资源
      最近更新 更多