×

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

Excel VBA DateDiff Function

The DateDiff function in VBA returns a Long data value representing the number of intervals between two specified dates/times where the type of interval is supplied by the user. Syntax DateDiff (Interval, Date1, Date2, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required)...

2 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 FormatPercent Function

VBA FormatPercent Function: The FormatPercent function in VBA is used to apply a percent format to a numeric expression, and it returns the result as a string. Syntax FormatPercent (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

VBA Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.

Excel VBA DateValue Function

The DateValue function in VBA returns a VBA Date from the given String representation of a date wherein the time information is ignored. It is unable to interpret dates that include the...

1 minute read.

Excel VBA DatePart Function

The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time. Syntax DatePart (Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required) – This parameter represents a string specifying the interval to be used. It can...

2 minutes read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA MonthName Function

Excel VBA MonthName Function: The MonthName function in VBA returns a string with the month name for the specified month number. Syntax MonthName (Month, [Abbreviate]) Parameter Month (required) – This parameter represents an integer between...

1 minute read.

ActiveX Controls

ActiveX Controls are one of the most used Excel Controls to automate applications with Excel VBA. It has the same controls, unlike Form Controls (Command Button, combo box, checkbox, etc.), but it...

3 minutes read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

Excel VBA- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position...

4 minutes read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

VBA UBound

VBA UBound  The UBound or Upper Bound function in VBA is used to specify the length of an array and returns the highest subscript for a dimension for the specified array. It is...

3 minutes read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA IsError Function

VBA IsError Function: The IsError function in VBA returns a Boolean value showing whether the specified Expression represents an error or not. Syntax IsError (Expression) Parameter Expression (required)- This parameter represents the variant that you...

1 minute read.

Excel VBA FormatCurrency Function

VBA FormatCurrency Function: The FormatCurrency function in VBA is used to apply a currency format to a numeric expression and returns the result as a string. Syntax FormatCurrency (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes 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.

Excel VBA : With End-with Constructs

With End-with Constructs The With-End With construct enables the user to perform multiple operations on a single object. If you are going to perform several different actions on the same object and typing the same...

4 minutes read.

Excel VBA: IF…..THEN …ELSE Statement

VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition...

2 minutes read.