组合两段VBA代码,用于PPT

组合两段VBA代码,用于PPT,vba,powerpoint,Vba,Powerpoint,我已经找到、更正并测试了用于在Powerpoint中处理图表的代码片段 第一步是使绘图大小相同并正确定位(假设幻灯片上有两个绘图),如下所示: Sub ProcessAllSlides() Dim sld As Slide Dim Shp As Shape For Each sld In ActivePresentation.Slides Call SetChartSizeAndPosition(sld.Shapes(1), 30, 120, 320, 240) Call S

我已经找到、更正并测试了用于在Powerpoint中处理图表的代码片段

第一步是使绘图大小相同并正确定位(假设幻灯片上有两个绘图),如下所示:

Sub ProcessAllSlides()

Dim sld As Slide
Dim Shp As Shape

For Each sld In ActivePresentation.Slides
    Call SetChartSizeAndPosition(sld.Shapes(1), 30, 120, 320, 240)
    Call SetChartSizeAndPosition(sld.Shapes(2), 370, 120, 320, 240)
Next

End Sub

Sub SetChartSizeAndPosition(Shp As Shape, Left As Single, Top As Single, Width As Single, Height As Single)

With Shp
    .Left = Left
    .Top = Top
    .Width = Width
    .Height = Height
End With

End Sub
这适用于演示文稿中的所有幻灯片。我希望它只在活动幻灯片上运行

第二个部分应该格式化绘图区域(而不是图表区域)。见下文:

Sub SizePlotArea()

Dim oSld As Slide
Dim oCht As Chart

Set oCht = ActiveWindow.Selection.ShapeRange(1).Chart

With oCht.PlotArea
    ' Edit these values as needed
    ' Change the following lines to e.g. Msgbox .Left etc
    ' to get the values of the chart you want to match others TO
    .Left = 0
    .Top = 0
    .Height = 220
    .Width = 300
End With

End Sub
理想情况下,我希望将两者结合起来,以便对活动幻灯片上的所有(两)个图表都这样做


任何人有任何提示吗?

请尝试下面的合并
子部分,代码注释中的解释

Option Explicit

Sub ProcessAllSlides()

Dim sld As Slide
Dim Shp As Shape
Dim oCht As Chart
Dim i As Long
Dim ChartIndex As Long

' set the Active Slide
Set sld = Application.ActiveWindow.View.Slide

ChartIndex = 1

' --- loop through the Slide shapes and search for the Shape of type chart
For i = 1 To sld.Shapes.Count
    If sld.Shapes(i).HasChart = msoTrue Then  ' if current shape is a chart
        Set Shp = sld.Shapes(i)
        Set oCht = Shp.Chart

        If ChartIndex = 1 Then ' first chart
            SetChartSizeAndPosition Shp, 30, 120, 320, 240
            ChartIndex = ChartIndex + 1

        ElseIf ChartIndex = 2 Then ' second chart
            SetChartSizeAndPosition Shp, 370, 120, 320, 240
        End If

        With oCht.PlotArea
            ' Edit these values as needed
            ' Change the following lines to e.g. Msgbox .Left etc
            ' to get the values of the chart you want to match others TO
            .Left = 0
            .Top = 0
            .Height = 220
            .Width = 300
        End With

        Set oCht = Nothing
        Set Shp = Nothing
    End If
Next i

End Sub

谢谢你。这就是我想要的——几乎是。由于某种原因,如果其他代码的一部分在另一个模块中,它就会运行。如果我只是单独运行代码,我会在“SetChartSizeAndPosition Shp,30,120,320,240”行中得到一个“编译错误”(未定义子函数或函数)。知道为什么吗?我知道了。当然,只需要上面的定义子项。对不起,谢谢你的回答