使用VBA在PowerPoint图表中选择新阵列?

时间:2018-08-24 13:19:53

标签: vba charts powerpoint powerpoint-vba

我在powerpoint中有一个图形,其中他的数据中有很多行,而我想用VBA做的只是用VBA选择一个特定的行,您能帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

稍微修改this answer

  1. 创建ComboBox1并将其中的"Bus 1"填充到"Bus 22"

Private Sub ComboBox1_DropButtonClick()
    Dim i As Integer

    With ComboBox1
        If .ListCount = 0 Then
            For i = 1 To 22
                .AddItem "Bus " & i, i - 1
            Next i
        End If
    End With
End Sub

  1. 然后代替隐藏行,实际上使用Chart.SetSourceData更改图表的源数据。要做到这一点:

    • 工具>引用下,添加对 Microsoft Excel对象库的引用。
    • FindComboBox1.Value上的Worksheet,并获得相应的源数据范围。
    • 然后使用SetSourceData和对源数据的Worksheet NameAddress的串联引用

Private Sub ComboBox1_Change()
    Dim shp As Shape: Set shp = ActivePresentation.SlideShowWindow.View.Slide.Shapes("Chart 5") ' Change to your chart name
    Dim rng As Excel.Range, dataRng As Excel.Range
    Dim ws As Excel.Worksheet: Set ws = shp.Chart.ChartData.Workbook.Sheets(1)

    With ws
        Set rng = .Cells.Find(ComboBox1.Value)

        If Not rng Is Nothing Then
            ' Data rng starts 1 column to the right and spans 2 rows
            Set dataRng = .Range(rng.Offset(, 1), rng.End(xlToRight).Offset(1))
            shp.Chart.SetSourceData Source:="='" & .Name & "'!" & dataRng.Address, PlotBy:=xlRows
        End If
    End With
End Sub