是否可以根据在 PowerPoint 用户窗体中输入的数据更新 'Chart In Microsoft PowerPoint' 中包含的数据

Is it possible to update data contained in a 'Chart In Microsoft PowerPoint' from data entered in a PowerPoint UserForm

图表是通过 Microsoft PowerPoint 中的图表 功能创建的,我们希望通过使用 PowerPoint 用户窗体来控制图表的更新方式。在执行例程时,是否有一行代码可以指向 Chart In Microsoft PowerPoint 中名为 DVPVreport 的 sheet?下面的当前代码不认为 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

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

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 的关联。