Steps to Create a Chart

Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to the object without an existing chart, you create a ChartObject on a given Worksheet and then get the Chart object from it.

We should have an excel numeric data before creating any chart. Here, we have taken the sample data.

Steps to Create a Chart in VBA

The below procedure explains the step by step creation of a simple bar Chart VBA program. We have used a few chart properties and features in the below code. But the user can modify or use the style or any other chart properties as per his requirement.

  1. Declare the chart variable

Create a new module and start the sub procedure. The next step is to declare the chart variable to return a single Chart object.

Program:

Steps to Create a Chart in VBA
  • Set the chart Variable

Now, as we have declared the chart object, we will set the chart object. The chart object is just a container that sets above all the ranges.  It floats above all the cells on the worksheets and holds the actual chart. To set the chart object we must use the word ‘set’ along with the variable chart object equal to Charts.Add (Set ChartVar = Charts.Add).

Program:

Steps to Create a Chart in VBA
  • Setting layout for Chart by ‘With’ statement

Using the ‘with’ statement to shorten the code length and defining as many properties within the with block. If you open a with statement you also have to end the with statement.

Program:

Steps to Create a Chart in VBA
  • Defining Source Data method

In the below program, we have used the set source data so as we can specify the data from the excel sheet to create the Chart.

Program:

Steps to Create a Chart in VBA
  • Setting Data Source Range

Specifying the range of Excel source data

Program:

  • Selecting Chart Type property.

Now we have to create our chart. It is the visual representation of the data that we see unlike the pie chart, bar chart, line chart, etc. It lies inside of the chart variable.

Program:

  • Changing the Tittle

We will apply a title for our chart. Firstly, we will enable the HasTitle property to TRUE. 

Program:

Steps to Create a Chart in VBA
  • Running the output

Once you are done with the code, press ALT + F5. You will notice in the footer of excel another sheet has been added with name Chart1 (default).

Steps to Create a Chart in VBA

Output

Steps to Create a Chart in VBA

Creating chart with chartObject

The below example is the alternative of the above program. We have used the chartobject method. Here, instead of opening another chart sheet window unlike, in the above example, it will be created within your excel sheet next to your data.  We have used the below code to set the dimensions of the chart window.

Set ChartVar = Wrksht.ChartObjects.Add(Left:=ActiveCell.Left, Width:=400, Top:=ActiveCell.Top, Height:=200)

Steps to Create a Chart in VBA

Even though you have specified the size, but you can still move the chart window manually once the chart is been created.

Program:

Steps to Create a Chart in VBA

Output

Steps to Create a Chart in VBA

Pin It on Pinterest

Share This