VBA HOME PAGE

Menu

Recording macros

Looking at the code

Ways of running macros

Where macros are stored

Reasons to write macros

Writing macros

Procedure types

Visual Basic editor (VBE)

Rules & conventions

Excel objects

Range/Selection objects

Object hierarchy

Object browser

Chart objects

Pivot Table objects

Formulas

Visual Basic Functions

Creating Add-Ins

Variables & constants

Object variables

Arrays

Collections

Message Box

VBA Input Box

Excel Input Box

Making decisions (If)

Making decisions (Case)

Looping (Do...Loop)

Looping (For...Loop)

With...End With blocks

User defined functions

Event handling

Error handling

Debugging

Creating User Forms

DAO/ADO Objects

Input/Output Files

 

Other links

Example code snippets

Userform input example

 

 

 

 

 


Charts Objects

When you add a chart when recording a macro, the code generated follows the menu and command that users manually call when adding a chart which means there is a discipline to run the macro in exactly the same way or face the potential of landing up with different results or even errors.

 

Typical code for a recorded macro:

Sub RecordedMacroChart()
'
' RecordedMacroChart Macro
'

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("'Sheet2'!$A$2:$D$12")
    ActiveChart.ChartType = xlLineMarkers
End Sub

The other issue with the above example code is typically the reference to the source data (which is an absolute string reference Sheet2'!$A$2:$D$12). The user may want more flexibility in controlling where this reference is by using and passing variables.

 

Note: Previous versions of Excel records macros using the object and method Charts.Add but it still gives the inflexibility in terms of control.

 

The above code generates a standard size chart within a worksheet and there is no room for setting properties until users edit the properties of an existing chart which just adds more code and becomes inefficient.

 Create Chart Objects

By creating your own written procedure and introduce ChartObjects keyword with supporting methods and properties, you have more control and can be flexible in passing arguments thus reducing extra lines of code.

 

An example:

Sub WrittenMacroChart()
'
' WrittenMacroChart Macro
'

   With ActiveSheet.ChartObjects.Add _
            (Left:=100, Width:=400, Top:=100, Height:=250)
        .Chart.SetSourceData Source:=Sheets("Sheet2").Range("A2:D12")
        .Chart.ChartType = xlXYScatterLines
    End With

End Sub

The above example allows the chart object to be positioned and sized (measured in pixels) accordingly using the Add method and its arguments.

 

Defining object variables for longer based procedures makes the code more clinical and efficient to write even though we must first declare a new object (as ChartObject).

Sub WrittenMacroChartObject()
    Dim
ChrtObj As ChartObject
    Set
ChrtObj = A
ctiveSheet.ChartObjects.Add _
            (Left:=100, Width:=400, Top:=100, Height:=250)
   
ChrtObj.Chart.SetSourceData Source:=Sheets("Sheet2").Range("A2:D12")
   
ChrtObj.Chart.ChartType = xlXYScatterLines
End Sub

The above example is the same as the previous code snippet but using the object variable ChrtObj as declared and set.

 

The other useful method is SetSourceData as you can add as many series as required (one at a time) enabling what ranges you want to set and not let Excel make as assumption.

 Adding Series

When recording a macro adding a series each line of code is created for a name, y-axis values and x-axis values if required using SeriesCollection and NewSeries keywords.

 

An example of recorded macro which adds three series (names and values) and an y-axis to an existing empty chart on a worksheet:

Sub AddingSeries()
     ActiveChart.SeriesCollection.NewSeries
     ActiveChart.SeriesCollection(1).Name = "='Sheet2'!$B$2"
     ActiveChart.SeriesCollection(1).Values = "='Sheet2'!$B$3:$B$12"
     ActiveChart.SeriesCollection.NewSeries
     ActiveChart.SeriesCollection(2).Name = "='Sheet2'!$C$2"
     ActiveChart.SeriesCollection(2).Values = "='Sheet2'!$C$3:$C$12"
     ActiveChart.SeriesCollection.NewSeries
     ActiveChart.SeriesCollection(3).Name = "='Sheet2'!$D$2"
     ActiveChart.SeriesCollection(3).Values = "='Sheet2'!$D$3:$D$12"
     ActiveChart.SeriesCollection(3).XValues = "='Sheet2'!$A$3:$A$12"

End Sub

Using the With...End With statement will refine the code and make it easier to understand. Also, introducing your own objects for a series just gives you better control should you wish to assign variables and arrays to it.

 

The same as the above example but a better practice:

Sub AddingSeriesObjects()

    Dim ChrtSrs1 As Series, ChrtSrs2 As Series, ChrtSrs3 As Series
   
Set ChrtSrs1 = ActiveChart.SeriesCollection.NewSeries
    With ChrtSrs1
        .Name = "='Sheet2'!$B$2"
        .Values = "='Sheet2'!$B$3:$B$12"
        .XValues = "='Sheet2'!$A$3:$A$12"
    End With
    Set ChrtSrs2 = ActiveChart.SeriesCollection.NewSeries
    With ChrtSrs2
        .Name = "='Sheet2'!$C$2"
        .Values = "='Sheet2'!$C$3:$C$12"
        .XValues = "='Sheet2'!$A$3:$A$12"
    End With
   
    Set ChrtSrs3 = ActiveChart.
SeriesCollection.NewSeries
    With ChrtSrs3
        .Name = "='Sheet2'!$D$2"
        .Values = "='Sheet2'!$D$3:$D$12"
        .XValues = "='Sheet2'!$A$3:$A$12"
    End With
End Sub

Remember, you can pass variables better into the above example code (not illustrated).

 

To delete a series use ActiveChart.SeriesCollection(1).Delete where the index (brackets with a 1) represents the first series for the active chart.

 

Using arrays and collections, you have better control especially when wanting to handle multiple charts in one go.

 

Data used for the above chart:

 Chart object looks something like:

 

 

 


Next Topic: Pivot Table objects

 

Want to teach yourself Access? Free online guide at About Access Databases

VBA Keyword: ActiveSheet, AddChart, Select, SetSourceData, With...End With, ChartType, ChartObjects, SeriesCollections, NewSeries & Set.
Home | Terms of Use | Privacy Policy | Contact

© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved.

All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy.