Elite Membership

VBA Charts

Written by Jeevan A Y Jeevan A Y Freelance Writer Jeevan, a seasoned data expert with 7 years in MIS reporting, excels in Advanced Excel, VBA, Power BI, and SQL. Currently an Assistant Manager MIS, his insightful data storytelling drives swift decision-making. 7+ years of experience MBA (Finance & Marketing) MIS Reporting View Full Profile
Reviewed by Dheeraj Vaidya, CFA, FRM Dheeraj Vaidya, CFA, FRM Content Reviewer & Course Director Dheeraj is a former J.P. Morgan and CLSA Equity Analyst with nearly two decades of experience in financial modeling, valuation, equity research, and corporate finance. He specializes in helping students and professionals develop practical and in-demand finance skills through structured and AI-powered, 20+ Years of experience CFA, FRM, IIT Delhi, IIM Lucknow Financial Modeling View Full Profile
Updated May 14, 2025
Read Time 5 min

Excel VBA Charts

We can term charts as objects in VBA. Similar to the worksheet, we can also insert charts in VBA. First, we select the data and chart type we want for our data. Now, there are two different types of charts we provide. One is the embed chart, where the chart is in the same sheet of data. Another is known as the chart sheet, where the chart is in a separate data sheet.

Download FREE VBA Charts In Excel Template and Follow Along!
Download Excel Template
VBA Charts

In data analysis, visual effects are the key performance indicators of the person who has done the analysis. Visuals are the best way an analyst can convey their message. Since we are all Excel users, we usually spend considerable time analyzing the data and drawing conclusions with numbers and charts. Creating a chart is an art to master. We hope you have good knowledge of creating charts with excel. This article will show you how to create charts using VBA coding.

How to Add Charts using VBA Codeย in Excel?

#1 – Create Chart using VBA Coding

To create any chart, we should have some numerical data. For this example, we are going to use the below sample data.

VBA Chart Example 1

First, let us jump to the VBA editor.

Step 1: Start Sub Procedure.

Code:

Sub Charts_Example1() End SubVBA Chart Example 1-1

Step 2: Define the variable as Chart.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart End SubVBA Chart Example 1-2

Step 3: Since the chart is an object variable, we need toย Setย it.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add End SubVBA Chart Example 1-3

The above code will add a new sheet as a chart sheet, not a worksheet.

VBA Chart Example 1-4

Step 4: Now, we need to design the chart. Open With Statement.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add ย With MyChart ย End With End SubVBA Chart Example 1-5

Step 5: The firstย thing we need to do with the chart is to Set the source range by selecting theย โ€œSet Source Dataโ€ย method.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add ย With MyChart ย .SetSourceData ย End With End SubVBA Chart Example 1-6

Step 6: We need to mention the source range. In this case, my source range is in the sheet named โ€œSheet1,โ€ and the range is โ€œA1 to B7โ€.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add ย With MyChart ย .SetSourceData Sheets(“Sheet1”).Range(“A1:B7”) ย End With End SubExample 1-7

Step 7: Next up, we need to select the kind of chart we are going to create. For this, we need to select theย Chart Typeย property.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add ย With MyChart ย .SetSourceData Sheets(“Sheet1”).Range(“A1:B7”) ย .ChartType = ย End With End SubVBA Chart Example 1-8

Step 8: Here, we have a variety of charts. I am going to select the โ€œxlColumnClusteredโ€ chart.

Code:

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add ย With MyChart ย .SetSourceData Sheets(“Sheet1”).Range(“A1:B7”) ย .ChartType = xlColumnClustered ย End With End SubExample 1-9

Now letโ€™s run the code using the F5 key or manually and see how the chart looks.

VBA Chart Example 1-10

Step 9: Now, change other properties of the chart. To change the chart title, below is the code.

Example 1-11

Like this, we have many properties and methods with charts. Use each one of them to see the impact and learn.

Sub Charts_Example1() ย Dim MyChart As Chart ย Set MyChart = Charts.Add ย With MyChart ย .SetSourceData Sheets(“Sheet1”).Range(“A1:B7”) ย .ChartType = xlColumnClustered ย .ChartTitle.Text = “Sales Performance” ย End With End Sub

#2 – Create a Chart with the Same Excel Sheet as Shape

We need to use a different technique to create the chart with the same worksheet (datasheet) as the shape.

Step 1: First, declare three object variables.

Code:

Sub Charts_Example2() ย Dim Ws As Worksheet ย Dim Rng As Range ย Dim MyChart As Object End SubVBA Chart Example 2

Step 2: Then, set the worksheet reference.

Code:

Sub Charts_Example2() ย Dim Ws As Worksheet ย Dim Rng As Range ย Dim MyChart As Object ย Set Ws = Worksheets(“Sheet1”) End SubExample 2-1

Step 3: Now, set the range object in VBA

Code:

Sub Charts_Example2() ย Dim Ws As Worksheet ย Dim Rng As Range ย Dim MyChart As Object ย Set Ws = Worksheets(“Sheet1”) ย Set Rng = Ws.Range(“A1:B7”) End SubExample 2-2

Step 4: Now, set the chart object.

Code:

Sub Charts_Example2() ย Dim Ws As Worksheet ย Dim Rng As Range ย Dim MyChart As Object ย Set Ws = Worksheets(“Sheet1”) ย Set Rng = Ws.Range(“A1:B7”) ย Set MyChart = Ws.Shapes.AddChart2 End SubExample 2-3

Step 5: Now, as usual, we can design the chart using the โ€œWithโ€ statement.

VBA Chart Example 2-4

Code:

Sub Charts_Example2() Dim Ws As Worksheet ‘To Hold Worksheet Reference Dim Rng As Range ‘To Hold Range Reference in the Worksheet Dim MyChart As Object Set Ws = Worksheets(“Sheet1”) ‘Now variable “Ws” is equal to the sheet “Sheet1” Set Rng = Ws.Range(“A1:B7”) ‘Now variable “Rng” holds the range A1 to B7 in the sheet “Sheet1” Set MyChart = Ws.Shapes.AddChart2 ‘Chart will be added as Shape in the same worksheet With MyChart.Chart .SetSourceData Rng ‘Since we already set the range of cells to be used for chart we have use RNG object here .ChartType = xlColumnClustered .ChartTitle.Text = “Sales Performance” End With End Sub

It will add the chart below.

VBA Chart Example 2-5

#3 – Code to Loop through the Charts

Like how we look through sheets to change the name, insert values, and hide and unhide them. Similarly, we need to use the ChartObject property to loop through the charts.

The below code will loop through all the charts in the worksheet.

Code:

Sub Chart_Loop() ย Dim MyChart As ChartObject ย For Each MyChart In ActiveSheet.ChartObjects ย ‘Enter the code here ย Next MyChart End Sub

#4 – Alternative Method to Create Chart

We can use the below alternative method to create charts. We can use the ChartObject. Add method to create the chart below is the example code.

It will also create a chart like the previous method.

Code:

Sub Charts_Example3() ย Dim Ws As Worksheet ย Dim Rng As Range ย Dim MyChart As ChartObject ย Set Ws = Worksheets(“Sheet1”) ย Set Rng = Ws.Range(“A1:B7”) ย Set MyChart = Ws.ChartObjects.Add(Left:=ActiveCell.Left, Width:=400, Top:=ActiveCell.Top, Height:=200) ย MyChart.Chart.SetSourceData Source:=Rng ย MyChart.Chart.ChartType = xlColumnStacked ย MyChart.Chart.ChartTitle.Text = “Sales Performance” End Sub

Recommended Articles

This article has been a guide to VBA Charts. Here, we learn how to create a chart using VBA code, practical examples, and a downloadable template. Below you can find some useful Excel VBA articles: –