Creating Line chart, from data every three lines.

Arithos

Well-known Member
Joined
Aug 14, 2014
Messages
598
Hi,

I'm trying to create a macro to make a line chart. However the buildup of the data creates some problems.

The Data:

CompanyDatePrice 1Price 2
110.09.20141204,991199,77
210.09.20141196,641191,45
310.09.20141196,681191,49
109.09.20141206,381203,29
209.09.20141198,051194,98
309.09.20141198,081195,01
108.09.20141202,831205,30
208.09.20141194,561197,02
308.09.20141194,571197,04
105.09.20141205,301199,49
205.09.20141197,111191,34
305.09.20141197,091191,34
104.09.20141202,601205,42
204.09.20141194,451197,26
304.09.20141194,441197,26
103.09.20141199,371197,59
203.09.20141191,301189,52
303.09.20141191,271189,51
102.09.20141187,201185,29
202.09.20141179,231177,33
302.09.20141179,211177,31
101.09.20141190,041189,27
201.09.20141182,091181,32
301.09.20141182,071181,30

<tbody>
</tbody>


What I want is to make three charts, one for Company 1, 2 and 3.

X-axis will have the Date as the value. and I want Price 1 and 2 to be displayed as a separate lines.

The problem arises when I want to select data from every three lines. I want to make it dynamic aswell, so that if I add new data, I can run the macro again, or it will update itself.

One thing that could help me on the way is either letting me know how to make a line-chart from an array. I populate it like this.

Code:
    LR1 = Cells(Rows.Count, 1).End(xlUp).Row
    LR = (LR1 / 3) + 1
    
    ReDim XArray(1 To LR, 1 To 2)
    ReDim Yarray(1 To LR)
    x = 1
    
For i = 1 To LR1 Step 3
    XArray(x, 1) = Cells(i, 3).Value
    XArray(x, 2) = Cells(i, 4).Value
    Yarray(x) = Cells(i, 2).Value
    x = x + 1
Next

But how to I create a chart from these values?

another option is this recorded piece:

Code:
    Range("B1:C30").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("'Global'!$B$11:$C$41")
    ActiveChart.ChartType = xlLine
    ActiveChart.Axes(xlCategory).Select
    ActiveSheet.ChartObjects("Chart 4").Activate
    Selection.TickLabels.NumberFormat = "dd.mm.åå;@"
    ActiveSheet.ChartObjects("Chart 4").Activate
    ActiveChart.Axes(xlValue).Select
    Selection.TickLabels.NumberFormat = "# ##0_ ;[Red]-# ##0\ "

I could manually select the data I want, but that's a nuisance when the data increase. Is there any way to "loop information to a range"? If so that could be a solution.

Any help appreciated :)
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
A more flexible approach is to convert your input data into a Table, so any added data expands the Table. Then use this Table as the source data for a pivot Table. Put Price 1 and Price 2 into the data area of the Pivot Table, put Company into the column area, and put the date into the row area. Finally make a pivot chart from the pivot table. The pivot table has one row per date, so the pivot chart makes sense.
 
Upvote 0
A more flexible approach is to convert your input data into a Table, so any added data expands the Table. Then use this Table as the source data for a pivot Table. Put Price 1 and Price 2 into the data area of the Pivot Table, put Company into the column area, and put the date into the row area. Finally make a pivot chart from the pivot table. The pivot table has one row per date, so the pivot chart makes sense.

Thanks for the idea, although based on your expertise wouldn't it (the pivot table) be a bit heavier (file size) than the formula and named ranges?
Also Pivot Chart are a feature reserved to PC right? Hence not compatible with a MAC although OP might probably uses Excel 2010...
 
Upvote 0
1. Really worried about file size? Office 2007-2013 files (including Office 2011) are essentially zipped; hard drive space costs what, pennies per gigabyte? And most everyone has broadband now and ginormous email accounts. And pivot tables are fast and flexible.

2. I didn't realize Macs don't do Pivot Charts. I use Mac Excel as little as possible, but I had to check it out. Hmmm. In any case, the pivot table can still be used to make a regular chart which is no less capable than one made from data that involves complicated formulas.
 
Upvote 0
1. Really worried about file size? Office 2007-2013 files (including Office 2011) are essentially zipped; hard drive space costs what, pennies per gigabyte? And most everyone has broadband now and ginormous email accounts. And pivot tables are fast and flexible.

2. I didn't realize Macs don't do Pivot Charts. I use Mac Excel as little as possible, but I had to check it out. Hmmm. In any case, the pivot table can still be used to make a regular chart which is no less capable than one made from data that involves complicated formulas.


This sounds interresting, I'll check the pivot option out. The amount of array formulas slows the sheet down abit, however that is not much of a problem since this sheet is mainly for statistical overview.

Thanks!
 
Upvote 0
For those who are interested, this ended as my solution.

Mix of recorded code and adjustments.

If anyone have simplifications/how I could manually enter the recorded parts, I would love to know (this is only for educational purposes).

Thanks for all the help!

Code:
Option Explicit


Sub MakeCharts2()
Application.ScreenUpdating = False


On Error Resume Next
Dim Piv As Worksheet, ChSht As Worksheet
    Sheets("Pivot3").Visible = True   'Data chart for pivot table
    Sheets("ChartSht3").Visible = True
    On Error GoTo 0
Set Piv = Sheets("Pivot3")
Set ChSht = Sheets("ChartSht3")
ChSht.Cells.Clear
Piv.Cells.Clear


Dim sht1 As Worksheet


Set sht1 = Sheets("Vekst")


sht1.Select
On Error Resume Next
    ActiveSheet.ChartObjects("Main").Delete
    ActiveSheet.ChartObjects("SkagA").Delete
    ActiveSheet.ChartObjects("SkagB").Delete
On Error GoTo 0


Piv.Cells.Clear


    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Vekst!Table3", Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:="Pivot3!R1C1", TableName:="PivotTable6", DefaultVersion _
        :=xlPivotTableVersion12
    Piv.Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("NAV Date")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("Fund")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
        "PivotTable6").PivotFields("NAV"), "Sum of NAV", xlSum
    ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
        "PivotTable6").PivotFields("Official NAV"), "Sum of Official NAV", xlSum
Dim LR As Integer
Dim Length As Integer, Witdth As Integer


LR = Piv.Cells(Rows.Count, 1).End(xlUp).Row


Length = LR - 3


Piv.Select
    Piv.Range(Cells(3, 1), Cells(LR - 1, 3)).Copy ChSht.Cells(1, 1)
    Piv.Range(Cells(3, 4), Cells(LR - 1, 5)).Copy ChSht.Cells(1, 6)
    Piv.Range(Cells(3, 6), Cells(LR - 1, 7)).Copy ChSht.Cells(1, 10)
    Piv.Range(Cells(3, 1), Cells(LR - 1, 1)).Copy ChSht.Cells(1, 5)
    Piv.Range(Cells(3, 1), Cells(LR - 1, 1)).Copy ChSht.Cells(1, 9)




ChSht.Select
On Error Resume Next
ActiveSheet.ChartObjects("Main").Delete
ActiveSheet.ChartObjects("SkagA").Delete
ActiveSheet.ChartObjects("SkagB").Delete
On Error GoTo 0


  Range("A1:C" & Length).Select


    ActiveSheet.Shapes.AddChart.Name = "Main"
    ActiveSheet.ChartObjects("Main").Activate
    
With ActiveChart
    .SetSourceData Source:=Range("'ChartSht'!$A$1:$C$" & Length)
    .ChartType = xlLine
    .ApplyLayout (1)
    .ChartTitle.Text = "Main Fund" '******************
    .Axes(xlValue).AxisTitle.Delete
    .SeriesCollection(1).Name = "=""NAV"""
    .SeriesCollection(2).Name = "=""Official NAV"""
End With


Range("E1:G" & Length).Select
    ActiveSheet.Shapes.AddChart.Name = "SkagA"
    ActiveSheet.ChartObjects("SkagA").Activate
    
    With ActiveChart
            .SetSourceData Source:=Range("'ChartSht'!$E$1:$G$" & Length)
            .ChartType = xlLine
            .ApplyLayout (1)
            .ChartTitle.Text = "Global A" '******************
            .Axes(xlValue).AxisTitle.Delete
            .SeriesCollection(1).Name = "=""NAV"""
            .SeriesCollection(2).Name = "=""Official NAV"""
    End With
    
    Range("I1:K" & Length).Select
        ActiveSheet.Shapes.AddChart.Name = "SkagB"
    ActiveSheet.ChartObjects("SkagB").Activate


With ActiveChart
    .SetSourceData Source:=Range("'ChartSht'!$I$1:$K$" & Length)
    .ChartType = xlLine
    .ApplyLayout (1)
    .ChartTitle.Text = "Global B" '******************
    .Axes(xlValue).AxisTitle.Delete
    .SeriesCollection(1).Name = "=""NAV"""
    .SeriesCollection(2).Name = "=""Official NAV"""
End With
   
ActiveSheet.ChartObjects("Main").Copy


sht1.Select
    Range("D1").Select
        ActiveSheet.Paste
        
ChSht.Select
    ActiveSheet.ChartObjects("SkagA").Copy


sht1.Select
    Range("G1").Select
        ActiveSheet.Paste
        
ChSht.Select
    ActiveSheet.ChartObjects("SkagB").Copy


sht1.Select
    Range("J1").Select
        ActiveSheet.Paste
          Sheets("Pivot3").Visible = False
    Sheets("ChartSht3").Visible = False
       
 Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,549
Members
449,170
Latest member
Gkiller

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