Consulting

Results 1 to 3 of 3

Thread: Dynamic chart with VBA

  1. #1
    VBAX Regular
    Joined
    Dec 2009
    Posts
    22
    Location

    Dynamic chart with VBA

    Hi,
    First post here, so please be gentle
    I have a chart which I want to create with VBA, because I'm using a template om which new (to be created sheets) are based.
    I have this code now but I can't get the values properly assigned, who can help me out?? .. Thanks
    [vba]Sub AddChart()

    ActiveSheet.ChartObjects.Add(25, 1350, 301.5, 155.25).Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=ActiveSheet.Range("B85"), PlotBy:=xlColumns


    ActiveWorkbook.Names.Add Name:="Jaar", RefersTo:= _
    "=OFFSET(Basisgegevens!$G$2,COUNTA(Basisgegevens!$G$2:$G26)-1,0,-MIN(Basisgegevens!$K$2,COUNTA(Basisgegevens!$G$2:$G$22)-1),1)"
    ActiveWorkbook.Names.Add Name:="Omzet", RefersTo:="Offset(Basisgegevens!Jaar, 0, 1)"
    ActiveWorkbook.Names.Add Name:="Resultaat", RefersTo:="Offset(Basisgevens!Jaar, 0, 2)"

    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Values = "=Basisgegevens!Omzet"
    ActiveChart.SeriesCollection(1).Name = "=""Omzet"""
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).XValues = "=Basisgegevens!Jaar"
    ActiveChart.SeriesCollection(2).Values = "=Basisgegevens!Resultaat"
    ActiveChart.SeriesCollection(2).Name = "=""Resultaat"""
    ActiveChart.Location Where:=xlLocationAsObject, Name:="2008"
    End Sub[/vba]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    1. Just a questiom whether the highlighted addresses are as intended (should they be the same?)
    [vba]ActiveWorkbook.Names.Add Name:="Jaar", RefersTo:= _
    "=OFFSET(Basisgegevens!$G$2,COUNTA(Basisgegevens!$G$2:$G26)-1,0,-MIN(Basisgegevens!$K$2,COUNTA(Basisgegevens!$G$2:$G$22)-1),1)" [/vba] 2. amend this from:
    [vba]ActiveWorkbook.Names.Add Name:="Omzet", RefersTo:="Offset(Basisgegevens!Jaar, 0, 1)" [/vba]
    to:
    [vba]ActiveWorkbook.Names.Add Name:="Omzet", RefersTo:="=Offset(Basisgegevens!Jaar, 0, 1)" [/vba]

    3. Amend this:
    [vba]ActiveWorkbook.Names.Add Name:="Resultaat", RefersTo:="Offset(Basisgevens!Jaar, 0, 2)"[/vba] to:
    [vba]ActiveWorkbook.Names.Add Name:="Resultaat", RefersTo:="=Offset(Basisgegevens!Jaar, 0, 2)"[/vba]
    Finally, take a look at the code below, which I think does the same thing, but in a way that makes it easier to see what you're doing when it needs adjusting.
    [vba]Sub AddChart()
    ActiveWorkbook.Names.Add Name:="Jaar", _
    RefersTo:="=OFFSET(Basisgegevens!$G$2,COUNTA(Basisgegevens!$G$2:$G26)-1,0,-MIN(Basisgegevens!$K$2,COUNTA(Basisgegevens!$G$2:$G$22)-1),1)"
    ActiveWorkbook.Names.Add Name:="Omzet", RefersTo:="=Offset(Basisgegevens!Jaar, 0, 1)"
    ActiveWorkbook.Names.Add Name:="Resultaat", RefersTo:="=Offset(Basisgegevens!Jaar, 0, 2)"
    With ActiveSheet.ChartObjects.Add(25, 1350, 301.5, 155.25).Chart
    '.SetSourceData Source:=ActiveSheet.Range("B85"), PlotBy:=xlColumns 'you probably don't need this line
    .ChartType = xlColumnClustered
    With .SeriesCollection.NewSeries
    .Values = "=Basisgegevens!Omzet"
    .Name = "=""Omzet"""
    .XValues = "=Basisgegevens!Jaar"
    End With
    With .SeriesCollection.NewSeries
    .Values = "=Basisgegevens!Resultaat"
    .Name = "=""Resultaat"""
    End With
    .Location Where:=xlLocationAsObject, Name:="2008"
    End With
    End Sub[/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Dec 2009
    Posts
    22
    Location
    Hi p45cal,

    Your code is indeed better readable and working as a charm!!
    Thanks for helping me out, great !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •