Declaring Embedded Line Chart Variables
It's been my experience that understanding the embedded chart object model can be difficult. Understanding how to declare variables based on that model can be even more difficult. Below is a guide that I refer to when attempting to isolate selected parts of the embedded chart object model. The guide uses a simple embedded line chart as an example. Each example can be put in a standard code module.
To name an embedded chart, first activate the chart (click on it once) and then run the following:
Sub NEC()
ActiveChart.Parent.Name = "Cht1"
End Sub
The example below declares the variable called "Cht" as a ChartObject. To select an embedded chart:
Sub SEC()
Dim ChtObj As ChartObject
Set ChtObj = ActiveSheet.ChartObjects("Cht1")
ChtObj.Select
End Sub
The following three examples isolate the first series for an embedded line chart:
Example 1 - declares the variable called "Cht" as a ChartObject. To select the source values for series one in an embedded line chart:
Sub SeriesEx1()
Dim ChtObt As ChartObject
Set ChtObj = ActiveSheet.ChartObjects("Cht1")
ChtObj.Chart.SeriesCollection(1).Values = Range("A1:A5")
End Sub
Example 2 - declares the variable called "Cht" as a Chart. To select the source values for series one in an embedded line chart:
Sub SeriesEx2()
Dim Cht As Chart
Set Cht = ActiveSheet.ChartObjects("Cht1").Chart
Cht.SeriesCollection(1).Values = Range("A1:A5")
End Sub
Example 3 - declares the variable called "Srs" as a SeriesCollection. To select the source values for series one in an embedded line chart:
Sub SeriesEx3()
Dim Srs As SeriesCollection
Set Srs = ActiveSheet.ChartObjects("Cht1").Chart.SeriesCollection
Srs(1).Values = Range("A1:A5")
End Sub
The three examples above show that the object model is built in a hierarchy, or levels, with ChartObject at the highest level i.e. ChartObject -> Chart -> SeriesCollection -> an so on.
The next two examples attempt to isolate the points for the first series for an embedded line chart. However, it's confusing because the first example refers to the series object while the second refers to the points object. When the macros are run, their results will appear in the immediate window of the visual basic editor.
Example 1 - declares the variable called "Srs" as a series object. To capture the value for the first point in series one in an embedded line chart (refers to the series object):
Sub PointsEx1()
Dim Srs As Series
Set Srs = ActiveSheet.ChartObjects("Cht1").Chart.SeriesCollection(1)
Debug.Print Srs.Values(1)
End Sub
Example 2 - declares the variable called "Pts" as a points object. To count the number of points in series one for an embedded line chart (refers to the points object):
Sub PointsEx2()
Dim Pts As Points
Set Pts = ActiveSheet.ChartObjects("Cht1").Chart.SeriesCollection(1).Points
Debug.Print Pts.Count
End Sub
Although not the same, the modeling for many of the other embedded chart types (column, bar, etc.) works very similar to the above.