Table of Contents
A pie chart is a useful visual tool for representing data in a circular graph, where the size of each slice corresponds to the proportion of the whole. In VBA, you can create a pie chart by using the “ChartObjects.Add” method and specifying the chart type as “xlPie”. This will create a new pie chart object that can be customized with data and formatting.
For example, to create a pie chart in VBA that shows the percentage of sales for different products, you can first define the data range and assign it to a variable. Then, you can use the Add method to insert the chart object, specify the chart type as “xlPie”, and set the data source to be the defined range. Finally, you can use various chart formatting methods to customize the appearance of the pie chart, such as adding a title and legend. This will result in a visually appealing pie chart that effectively presents the sales data. With the flexibility and functionality of VBA, creating a pie chart can be easily automated and customized to suit your specific needs.
Create a Pie Chart in VBA (With Example)
You can use the following basic syntax to create a pie chart in Excel by using VBA:
Sub CreatePieChart()
Dim MyChart As ChartObject
'get input range from user
Set Rng = Application.InputBox(Prompt:="Select chart input range", Type:=8)
'create pie chartSet MyChart = Worksheets("Sheet1").ChartObjects.Add(Left:=ActiveCell.Left, _
Width:=400, Top:=ActiveCell.Top, Height:=300)
MyChart.Chart.SetSourceData Source:=Rng
MyChart.Chart.ChartType = xlPie
End Sub
This particular macro will prompt the user for an input range, then automatically generate a pie chart using the input range and insert it into the sheet called Sheet1 with the top left corner of the chart located in the currently active cell.
The following example shows how to use this macro in practice.
Example: How to Create a Pie Chart in VBA
Suppose we have the following dataset in Excel that contains information about points scored by various basketball players:

Suppose we would like to use VBA to generate a pie chart using this dataset.
We can create the following macro to do so:
Sub CreatePieChart()
Dim MyChart As ChartObject
'get input range from user
Set Rng = Application.InputBox(Prompt:="Select chart input range", Type:=8)
'create pie chartSet MyChart = Worksheets("Sheet1").ChartObjects.Add(Left:=ActiveCell.Left, _
Width:=400, Top:=ActiveCell.Top, Height:=300)
MyChart.Chart.SetSourceData Source:=Rng
MyChart.Chart.ChartType = xlPie
End SubTo run this macro, we can click on the Developer tab along the top ribbon in Excel, then click Macros.
We can then click the one titled CreatePieChart and then click Run:
Once we click Run, we will be prompted for an input range for our bar chart:

We will type A1:B7, then press OK.
The following pie chart will automatically be created and displayed with the top left corner of the chart located in the currently active cell, which happens to be cell D2:

Note: You can change the values for the Width and Height arguments in the ChartObjects.Add() function to adjust the width and height of the pie chart, respectively.
Cite this article
stats writer (2024). How can I create a pie chart in VBA, and what is an example of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-in-vba-and-what-is-an-example-of-how-to-do-so/
stats writer. "How can I create a pie chart in VBA, and what is an example of how to do so?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-in-vba-and-what-is-an-example-of-how-to-do-so/.
stats writer. "How can I create a pie chart in VBA, and what is an example of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-in-vba-and-what-is-an-example-of-how-to-do-so/.
stats writer (2024) 'How can I create a pie chart in VBA, and what is an example of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-in-vba-and-what-is-an-example-of-how-to-do-so/.
[1] stats writer, "How can I create a pie chart in VBA, and what is an example of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I create a pie chart in VBA, and what is an example of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
