Pages

Copy Excel Line Chart (Graph) Using VBA

From this lesson you can learn how to copy a chart using VBA. Let’s consider this sample excel line chart. Chart is on an activesheet.


Assume we want to create a copy of that chart in the same sheet. We can do that as follows.

Sub CopyChart()

     Dim WS As Worksheet

     Set WS = ActiveSheet

     WS.ChartObjects("Chart 4").Chart.ChartArea.Copy
     WS.Range("N5").Select
     WS.Paste

End Sub

In the above macro we used “WS.Range("N5").Select” to select the N5 cell before paste the chart. Therefore the chart's top left corner was positioned on that cell.

However If the user has selected the chart or if the ChartObject(which contains the chart) has activated, then we can create a copy of the chart like this.

Sub CopyChart_Ex2()

     ActiveChart.ChartArea.Copy
     Range("I10").Select
     ActiveSheet.Paste

End Sub

Now let’s look at how we can copy charts to a different sheet. Assume we have a workbook with two sheets called “Sheet1” and “Sheet2” in “Sheet1” containing a chart named “Chart 4”. Below macro will copy “Chart 4” from “Sheet1” to “Sheet2”.

Sub CopyChartToDifferentSheet()

     Dim WS_Current As Worksheet
     Dim WS_New As Worksheet

     Set WS_Current = Worksheets("Sheet1")
     Set WS_New = Worksheets("Sheet2")

     WS_Current.ChartObjects("Chart 4").Chart.ChartArea.Copy
     WS_New.Paste

End Sub