如何在 PowerPoint 中为活动图表设置特定数据源

问题描述 投票:0回答:1

我正在尝试与活动的 PowerPoint 演示文稿和数据透视表所在的工作簿中的特定工作表建立连接。 xxlsm 和 xpptm 文件已打开,在 PowerPoint 中,我在末尾添加了一张新幻灯片,并且正在创建饼图 - 好的,但是我希望此饼图包含我的工作簿和特定工作表的引用,而不是默认一个。

我的想法是尝试 ActiveChart.SetSourceData 并尝试用我的工作簿和工作表“枢轴”替换为范围(A2:B7),或者可能有其他选项?

'adding new slide
Set NewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, 11)

' Create the chart and set a reference to the chart data
Set myChart = ActivePresentation.Slides(ActivePresentation.Slides.Count).Shapes.AddChart.Chart

ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData **Source:=Sheets("Sheet1").Range("A1:A4")**, PlotBy:= _
    xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
ActiveChart.HasTitle = False

' 是否可以将螺栓源更改为我的工作簿和带有范围(A2:B7)的工作表“枢轴”?或者如果没有其他关于如何连接的想法?

excel vba powerpoint
1个回答
0
投票

在 Excel 中创建饼图,然后复制/粘贴到 PowerPoint 链接该图表。

Option Explicit
Sub demo()
    Dim xlApp As Object, xlWK As Object
    Dim xlChart As Object, xlSht As Object
    Dim oSlide As Slide, sWorkBook As String
    sWorkBook = "Data.xlsm"   ' Update your workbook name
    Set xlApp = GetObject(, "Excel.Application")
    If Not xlApp Is Nothing Then
        Set xlWK = xlApp.Workbooks(sWorkBook)
        If Not xlWK Is Nothing Then
            Set xlSht = xlWK.Sheets("pivot")
            xlSht.Range("A2:B7").Select
            Set xlChart = xlSht.Shapes.AddChart2(251, xlPie)
            xlChart.Copy
            With ActivePresentation
                Set oSlide = .Slides.Add(.Slides.Count + 1, 11)
            End With
            oSlide.Shapes.Paste
            xlChart.Delete
        End If
    End If
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.