Pages

Create line chart with multiple series using Excel VBA

From one of our earlier posts we learnt how to create a line chart with one series automatically using VBA. Read this post if you want to know how.

How to create a line chart automatically - Excel VBA

In this post I will show you how we can create line charts with multiple series using VBA. Let’s consider this sample table. For this data we need to create a line chart with 2 series.


As you can see we have our data in range A1:C13. So when this sheet is the active sheet, we can create 2 series line chart for this data as follows.

Sub CreateChart_2Series()

     Dim WS As Worksheet

     Set WS = ActiveSheet

     WS.Range("A1:C13").Select
     ActiveSheet.Shapes.AddChart2(227, xlLine).Select

End Sub

Note that you need to replace the A1:C13 with the range you want to create the line chart for. Here is the result of the above macro.

This is another sample table for which we can create a line chart with 3 series.

To create a line chart for all the data, the only change we need to do for our first macro is changing the range from A1:C13 to A1:D13.

Sub CreateChart_3Series()

     Dim WS As Worksheet

     Set WS = ActiveSheet

     WS.Range("A1:D13").Select
     ActiveSheet.Shapes.AddChart2(227, xlLine).Select

End Sub

Line chart with 3 series.