是否可以从PowerPoint用户窗体中输入的数据更新“Microsoft PowerPoint中的图表”中包含的数据

时间:2017-05-11 05:10:29

标签: excel-vba powerpoint userform vba excel

图表是通过图表在Microsoft PowerPoint 功能中创建的,我们希望通过使用PowerPoint用户表单来控制图表的更新方式。当执行例程时,是否有一行代码可以指向位于 Chart in Microsoft PowerPoint 中的名为DVPVreport的工作表?当前代码低于此值不考虑DVPVreport位于Microsoft PowerPoint中的图表中。我们试图执行代码:

Set ws = 'Chart In MicrosoftPowerPoint!'.Worksheets(DVPVreport) 

但没有成功。

Private Sub AddDVSetUp_Click()

   Dim ws As Worksheet
   Set ws = Worksheets("DVPVreport")

   ws.Cells(3, 4).Value = Gate2Date.Value
   Unload M

   ws.Cells(3, 5).Value = Gate3Date.Value
   Unload M

End Sub

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。试试这个:

Dim sl As Slide
Set sl = ActivePresentation.Slides(1)

Dim xlWB As Object
Dim sh As Shape, ch As Chart

For Each sh In sl.Shapes
    If sh.Type = msoChart Then
        Set ch = sh.Chart
        Set xlWB = ch.ChartData.Workbook
        With xlWB.Sheets(1) '/* It has only 1 sheet so this will be fine */
            '/* Do the changes you want here */
            .Range("B2").Value = 4.2
        End With
    End If
Next
Set ch = Nothing: Set xlWB = Nothing: Set sl = Nothing '/* clean up */

现在,如果您已经知道对象的索引或名称,则可以简单地:

Dim sl As Slide
Set sl = ActivePresentation.Slides(1)

Dim xlWB As Object
Dim sh As Shape

Set sh = sl.Shapes("Chart 1")
Set xlWB = sh.Chart.ChartData.Workbook

With xlWB.Sheets(1)
    .Range("B2").Value = 4.2
End With

重要提示:请注意不要破坏源数据布局。当您执行此操作时,由于某种原因,它会将图表与ChartData解除关联。