cellMatrix.net
Spreadsheet Modeling and Related Topics

About

Formulas, Charts, and Models Created with Microsoft Excel.
More . . .

Statistics

  • Total Entries - 102
  • Current Viewers - 15

Categories

Recent Comments

Syndicate

Validate

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.



You should get out of the habit of using a variable named Cht for both Charts and ChartObjects, because this will help confuse you. Use Cht and ChtOb, so it will be easier to “debug by eyeball”. Same with Srs for Series and for SeriesCollection.

In any case, I never use a variable for a series collection, because there’s only one series collection per chart; therefore it’s not a variable once I know which chart I’m talking about. Likewise I don’t use a variable for points, I would just use Srs.Points.Count (where Srs is a series, of course).

If you get in the habit of using a few variables regularly (for me it’s ChtOb, Cht, Srs, Axs for Axis, and rarely any others), you will leave any confusion behind.

Posted by Jon Peltier  on  08/28  at  07:19 PM


Comment Form

Name: (Required)
E-Mail Address: (Optional)
Location: (Optional)
Web Site Address: (Optional)

Remember my personal information?
Notify me of follow-up comments?

Before submitting your comment, please enter the phrase you see below: