Add series to chart in VBA

derthorsten

New Member
Joined
Jul 13, 2015
Messages
7
Hi,

I cant find a solution to the following problem:

I have a macro that creates a chart from data with one series "total". Its not the most efficient piece of code I guess but it does the job (if you have an idea how to shorten the code be by guest :-)).

yy_total is my complete data range consisting of 2 columns with X and Y values.

Code:
    Charts.Add
    With ActiveChart
        .ChartType = xlLine
        .HasTitle = True
        .HasLegend = False
        .ChartTitle.Text = "YY Total"
        .SetSourceData Source:=yy_total
        .Location Where:=xlLocationAsObject, Name:=ShName1
    End With
        With ActiveChart.Axes(xlCategory)
        .TickLabels.NumberFormat = "MMMMM/YY"
        End With
        With ActiveChart.Axes(xlValue)
        .MaximumScale = max_yytotal + 1000
        .MinimumScale = min_yytotal - 1000
        End With
            With ActiveChart.Parent
                .Top = Cells(4, 6).Top
                .Left = Cells(4, 6).Left
            End With

How can I add a second series to the chart? All my attempts have failed so far :confused:
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try...

Code:
    Dim oShape As Shape    

    Set oShape = ActiveSheet.Shapes.AddChart( _
        XlChartType:=xlLine, _
        Left:=Cells(4, 6).Left, _
        Top:=Cells(4, 6).Top)
        
    With oShape
        .Name = ShName1
        With .Chart
            .HasTitle = True
            .HasLegend = False
            .ChartTitle.Text = "YY Total"
            .SetSourceData Source:=Range("yy_total")
            .Axes(xlCategory).TickLabels.NumberFormat = "MMMMM/YY"
            With .Axes(xlValue)
                .MinimumScale = min_yytotal - 1000
                .MaximumScale = max_yytotal + 1000
            End With
        End With
    End With

Hope this helps!
 
Last edited:
Upvote 0
That's probably because of the values I'm assuming you've set for min_yytotal and max_yytotal and consequently for the minimum and maximum scales.
 
Upvote 0
That's probably because of the values I'm assuming you've set for min_yytotal and max_yytotal and consequently for the minimum and maximum scales.

I was thinking that there is only

Code:
.SetSourceData Source:=Range("yy_total")

and yy_total contains a date column and a value column. But where is the second series such as "zz_total"? That's what I m looking for.
 
Last edited:
Upvote 0
Sorry, but I'm a little confused. Do you want "zz_total" included in the present code? Or do you want to be able to run a separate code just to add "zz_total"?
 
Last edited:
Upvote 0
In that case, try...

Code:
.SetSourceData Source:=Range("yy_total,zz_total")

Or, you can define a name for the entire range of data, let's say you name it "ChartRange", and assign it as follows..

Code:
.SetSourceData Source:=Range("ChartRange")
 
Upvote 0
In that case, try...

Code:
.SetSourceData Source:=Range("yy_total,zz_total")

Or, you can define a name for the entire range of data, let's say you name it "ChartRange", and assign it as follows..

Code:
.SetSourceData Source:=Range("ChartRange")

and how can I select one of the two series separately then? Such as series yy_total should be blue, and series zz_total orange?
 
Upvote 0
Try...

Code:
    Dim oShape As Shape
    
    Set oShape = ActiveSheet.Shapes.AddChart( _
        XlChartType:=xlLine, _
        Left:=Cells(4, 6).Left, _
        Top:=Cells(4, 6).Top)
        
    With oShape
        .Name = ShName1
        With .Chart
            .HasTitle = True
            .HasLegend = False
            .ChartTitle.Text = "YY Total"
            .SetSourceData Source:=Range("yy_total,zz_total")
            .Axes(xlCategory).TickLabels.NumberFormat = "MMMMM/YY"
            With .Axes(xlValue)
                .MinimumScale = min_yytotal - 1000
                .MaximumScale = max_yytotal + 1000
            End With
            [COLOR=#ff0000].SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(0, 112, 192)[/COLOR] 'blue
           [COLOR=#ff0000] .SeriesCollection(2).Format.Line.ForeColor.RGB = RGB(255, 192, 0)[/COLOR] 'orange
        End With
    End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top