VBA charts in VBA in Excel

In this session, we will see, how to use the chart with VBA. To have an overview of all possible charts in Excel please see the Excel Chart Tutorial

Two types of charts can be worked with in VBA.

1.Chart Sheet

2.Embedded Chart

A chart sheet is a separate sheet in your workbook, with its own tab.

An embedded chart is one which is inserted within a sheet.

Let us consider the following data for illustration purpose.

excel vba charts in vba

Create a Chart sheet in VBA

The "Chart" object is used to create a new Chart sheet. The following code illustrates this:

  1. Sub createchartsheet()
  2. Dim ch As Chart
  3. Set ch = Charts.Add
  4. With ch
  5. .SetSourceData Source:=Sheets("Q51").Range("A11:B18")
  6. End With
  7. End Sub

As seen from the above example, the chart object has various properties which can be used to define a meaningful chart.

The "SetSourceData" is used to specify the source range for the chart.

The "Chart" object has various other properties, some of which are as discussed below.

HasTitle

This property is used to set title of the chart.

Before specifying a title, this property has to be set true.

ChartTitle

This is used to specify the title of the chart after the "HasTitle" is enabled.

The following example illustrates this

  1. Sub chart2()
  2. Dim ch As Chart
  3. Set ch = Charts.Add
  4. With ch
  5. .SetSourceData Source:=Sheets("Q51").Range("A11:B18")
  6. .HasTitle = True
  7. .ChartTitle.Text = "Population"
  8. End With
  9. End Sub
Axes

Now, to set a label or title for X and Y axes, the "Axes" property is used.

This is illustrated as below

  1. Sub chart3()
  2. Dim ch As Chart
  3. Set ch = Charts.Add
  4. With ch
  5. .SetSourceData Source:=Sheets("Q51").Range("A11:B18")
  6. .HasTitle = True
  7. .ChartTitle.Text = "Population"
  8. .Axes(xlCategory, xlPrimary).HasTitle = True
  9. .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = Sheets("Q51").Range("A11")
  10. End With

An Embedded chart is same as chart sheet, except that, they are created in the existing worksheet itself.

The following code demonstrates the embedded chart:

  1. Sub embeddedcht()
  2. Dim echt As Chart
  3. Set echt = Sheets("Q51").Shapes.AddChart.Chart
  4. With echt
  5. .SetSourceData Source:=Sheets("Q51").Range("A11:B18")
  6. .HasTitle = True
  7. .ChartTitle.Text = "Population"
  8. .Axes(xlCategory, xlPrimary).HasTitle = True
  9. .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = Sheets("Q51").Range("A11")
  10. End With
  11. End Sub

The screenshot of VBA Editor and outputs are as shown below:

excel vba charts in vba

excel vba charts in vba

Outputs:

excel vba charts in vba

 

You can find similar Excel Questions and Answer hereunder

1) VBA clear the contents of an entire sheet in Excel

2) How can I shade alternate rows in color using VBA to make it easier to read voluminous data running into hundreds of rows?

3) How do I update my DropDown list whenever the sheet is activated?

4) How to concatenate strings in VBA in Excel

5) Userform initialize vs userform show in Excel

6) How to display messages boxes in VBA with the msgbox function

7) Is it possible to use 2 labels in X-Axis of a chart (like both Month and Date, Date and Day, Date and Shift)?

8) How can I set up ListBox using VBA to allow users to select multiple values?

9) How can I add a WorkSheet and name it as required using VBA?

10) VBA code to password protect workbook in Excel

 

Here the previous and next chapter