×

Steps to Create a Chart in VBA

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:

Sub ChartsExample()
  'Declaring the chart variable
  Dim ChartVar As Chart
End Sub
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:

Sub ChartsExample()
  'Declaring the chart variable
  Dim ChartVar As Chart
  'setting the variable chart object
  Set ChartVar = Charts.Add
End Sub
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:

Sub ChartsExample()
  'Declaring the chart variable
  Dim ChartVar As Chart
  'setting the variable chart object
  Set ChartVar = Charts.Add
  With ChartVar
    'closing the with statement
  End With
End Sub
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:

Sub ChartsExample()
  'Declaring the chart variable
  Dim ChartVar As Chart
  'setting the variable chart object
  Set ChartVar = Charts.Add
  With ChartVar
    'closing the with statement
    .SetSourceData
  End With
End Sub
Steps to Create a Chart in VBA
  • Setting Data Source Range

Specifying the range of Excel source data

Program:

Sub ChartExample()
  Dim ChartVar As Chart
  Set ChartVar = Charts.Add
  With ChartVar
          . SetSourceData Sheets("Sheet1").Range("A1:B7")
  End With
End Sub
  • 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:

Sub ChartExample()
  Dim ChartVar As Chart
  Set ChartVar = Charts.Add
  With ChartVar
          . SetSourceData Sheets("Sheet1").Range("A1:B7")
          .ChartType = xlColumnClustered
  End With
End Sub
  • Changing the Tittle

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

Program:

Sub ChartsExample()
  'Declaring the chart variable
  Dim ChartVar As Chart
  'setting the variable chart object
  Set ChartVar = Charts.Add
  With ChartVar
    'closing the with statement
    .SetSourceData Sheets("Sheet1").Range("A2:B11")
    .HasTitle = True
    .ChartTitle.Text = "Sales Performance"
  End With
End Sub
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:

Sub ChartsExample()
    Dim Wrksht As Worksheet
    Dim Rng As Range
    Dim ChartVar As ChartObject
    'defining the worksheet value
    Set Wrksht = Worksheets("Sheet1")
    'defining the worksheet chart range
    Set Rng = Wrksht.Range("A2:B11")
    'Setting the CharVar variable and defining the chart frame dimensions.
    Set ChartVar = Wrksht.ChartObjects.Add(Left:=ActiveCell.Left, Width:=400, Top:=ActiveCell.Top, Height:=200)
    With ChartVar.Chart
        .SetSourceData Rng
        .ChartType = xlColumnClustered
        .HasTitle = True
        .ChartTitle.Text = "Sales Performance"
    End With
End Sub
Steps to Create a Chart in VBA

Output

Steps to Create a Chart in VBA

Related Topics

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.

VBA Regex Pattern Validation

VBA Regex Pattern Validation The key purpose of using Regex in VBA was to validate the data and extract the same category of data. There are certain formats that are standard...

6 minutes read.

Excel VBA Tutorial

What is VBA? Introduction to Excel VBA: Visual Basic for Applications (VBA) is a programming language developed by Microsoft to automate operations in applications, such as Excel, Word, PowerPoint, etc. It...

5 minutes read.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.

VBA Object Required

What is Object Required Error? VBA Object Required is a run time error which occurs when the user does not define a valid object qualifier, or the assigned object doesn’t exist in the...

6 minutes read.

Excel VBA Str Function

VBA Str Function: The Str function in VBA converts the given number into a string representation of that number. Syntax Str (Number) Parameter Number (required) – This parameter represents the numeric value that you want...

1 minute read.

VBA Option Explicit

VBA Option Explicit The Option Explicit in VBA is used to declare the variables at the top of your macro code. It is the most secure and easy option to maintain your variables....

5 minutes read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

Excel VBA Asc Function

VBA Asc Function: The Asc function in VBA returns an integer showing the character code for the first character of a given string. Syntax Asc (String) Parameter String (required) – This parameter represents the text...

1 minute read.

Excel VBA INT Function

VBA INT Function: The INT function in VBA rounds the given supplied number down and returns an integer value. The positive numbers are rounded to zero, and the negative numbers are rounded away from zero. Syntax Int...

1 minute read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

VBA Pivot Table

What is a Pivot Table? One of the most powerful features of Excel VBA is the Pivot Table. A pivot table is a VBA tool that is used to create summary...

3 minutes read.

Excel VBA Timer Function

Excel VBA Timer Function: The Timer function in VBA returns a Single data type, evaluating the number of seconds that have elapsed since midnight of the current day. Syntax Timer () Parameter NA Return This...

1 minute read.

Excel VBA: IF THEN Statement

VBA Excel: IF THEN StatementThis conditional statement enables you to check one condition and on the basis of that then run one or multiple statements if the condition holds. If...

1 minute read.

Excel VBA Right Function

VBA Right Function: The Right function in VBA returns a substring from the end of the given string. Syntax Right (Str, Length) Parameter Str (required) – This parameter represents the string from which you want to...

1 minute read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Excel VBA StrConv Function

The StrConv function in VBA converts a string into a specified format. Syntax StrConv (String, Conversion, [LocaleID]) Parameter String (required) – This parameter represents the string to be converted. Conversion (required) – This parameter specifies the type of conversion. It can...

1 minute read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

CodeIgniter Architecture

Here we will understand the architecture and working of the CodeIgniter application, which helps you to elaborate all steps in simple ways. As the above image represents that whenever a request comes from the...

2 minutes read.