0
votes

I want to be able to use my own Excel Workbook to create a pie diagram in PowerPoint. So far, I have created a VBA script to add the chart. I also changed the data sheet for the chart to look like this: Problem

Following core problem remains:

  • I can't figure out, how to dynamically define the data range of the chart's data source sheet

Because the default values for the pie chart belong to these ranges, PowerPoint sets those ranges by default. If my data sheet contains more (or equal to) than 4 rows of data, it won't be that bad, as the range automatically increases. However, as shown in the example, the range doesn't adapt when there are only 3 rows of data.

What I want, would look like this: Needed (I only know how to get the data in the correct place, but not how to select it)

Related VBA code is below:

Set diaPie = pres.Slides(3).Shapes.AddChart2(2, xlPie).Chart
Set pieChartData = diaPie.ChartData
Set pieWorkbook = pieChartData.Workbook
Set pieWorksheet = pieWorkbook.Worksheets(1)

pieWorksheet.UsedRange.Clear

With dataWorksheet
    id = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

a = dataWorksheet.Range("A1", dataWorksheet.Range("A1").Offset(id, 0))
b = dataWorksheet.Range("B1", dataWorksheet.Range("B1").Offset(id, 0))


pieWorksheet.Range("A2", pieWorksheet.Range("A2").Offset(id, 0)) = a
pieWorksheet.Range("B2", pieWorksheet.Range("B2").Offset(id, 0)) = b

Thanks in advance!

1
Can you simply link the objects ? Pie chart to range?QHarr

1 Answers

0
votes

This should change the data range on your data sheet. maxrow is just the end of the data. So if your data is in B1:B4 then maxrow would be 4

diaPie.SeriesCollection(1).values = "Sheet1!$B$1:$B" & maxrow  ' change the series range before opening and closing the workbook object
diaPie.SeriesCollection(1).XValues = "Sheet1!$A$1:$A" & maxrow  'Labels