+ Reply to Thread
Results 1 to 8 of 8

Thread: graphing a chart in VBA, showing series legends on the chart

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    graphing a chart in VBA, showing series legends on the chart

    Hi guys,

    I'm trying to generate a graph in VBA, but am having some difficulties getting the series legend on the graph.

    Can anybody help?

    Private Sub GraphMe_Click()
        Dim intSeries As Integer
        Dim dTop As Double
        Dim dLeft As Double
        Dim dHeight As Double
        Dim dWidth As Double
        Dim nColumns As Long
        
        Range(Cells(2, 3), Cells(13, 3)).Name = "datalegends"
        
        dTop = 75      ' top of first row of charts
        dLeft = 300    ' left of first column of charts
        dHeight = 225  ' height of all charts
        dWidth = 375   ' width of all charts
        nColumns = 3   ' number of columns of charts
    
    Application.ScreenUpdating = False
    Charts.Add
    
    
    With ActiveChart
    For intSeries = 2 To .SeriesCollection.Count
    .SeriesCollection(intSeries).Name = Range("datalegends").Cells(intSeries, 3)
    Next
    
    
    .Axes(xlCategory, xlPrimary).HasTitle = True 'set the switch on first
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlCategory).HasMajorGridlines = True
    .Axes(xlValue).HasMajorGridlines = True
    .PlotArea.Interior.ColorIndex = 2
    .PlotArea.Interior.PatternColorIndex = 1
    .HasTitle = True 'set the switch on first
    .ChartTitle.Characters.Text = "Capacity"
    .HasLegend = True
    .ChartType = xlXYScatterLines
    .SetSourceData Source:=Sheets("Sheet1").Range("B2:B13")
    .Location Where:=xlLocationAsObject, Name:="Sheet1"
    
    End With
    
    
    ActiveWindow.RangeSelection.Activate
    Application.ScreenUpdating = True
    End Sub
    So I think that for loop is what I want shown on the x-axis on the graph, but months are not showing when run.

    Any ideas?
    Attached Files Attached Files
    Last edited by Lifeseeker; 02-09-2012 at 08:58 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: graphing a chart in VBA, showing series legends on the chart

    Is anybody able to assist me on this?

    I'm almost there...

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: graphing a chart in VBA, showing series legends on the chart

    The chart type xy-scatter only displays numeric values on the axes.
    If you want months on the x axis you will need to use a Line chart.

    Private Sub GraphMe_Click()
        Dim intSeries As Integer
        Dim dTop As Double
        Dim dLeft As Double
        Dim dHeight As Double
        Dim dWidth As Double
        Dim nColumns As Long
        Dim rngData As Range
        Dim rngLabels As Range
        Dim rngName As Range
        
        With ActiveSheet
            Set rngData = .Range(.Cells(2, 2), .Cells(13, 2))
            Set rngLabels = .Range(.Cells(2, 3), .Cells(13, 3))
            Set rngName = .Cells(1, 2)
        End With
        
        dTop = 75      ' top of first row of charts
        dLeft = 300    ' left of first column of charts
        dHeight = 225  ' height of all charts
        dWidth = 375   ' width of all charts
        nColumns = 3   ' number of columns of charts
    
    Application.ScreenUpdating = False
    Charts.Add
    
    
    With ActiveChart
        .ChartType = xlLineMarkers
        Do While .SeriesCollection.Count > 0
            ' remove excess series
            .SeriesCollection(.SeriesCollection.Count).Delete
        Loop
        With .SeriesCollection.NewSeries
            .Values = rngData
            .XValues = rngLabels
            .Name = rngName
        End With
        
        .Axes(xlCategory, xlPrimary).HasTitle = True 'set the switch on first
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlCategory).HasMajorGridlines = True
        .Axes(xlValue).HasMajorGridlines = True
        .PlotArea.Interior.ColorIndex = 2
        .PlotArea.Interior.PatternColorIndex = 1
        .HasTitle = True 'set the switch on first
        .ChartTitle.Characters.Text = "Capacity"
        .HasLegend = True
        .Location Where:=xlLocationAsObject, Name:=rngData.Parent.Name
    
    End With
    
    
    ActiveWindow.RangeSelection.Activate
    Application.ScreenUpdating = True
    End Sub
    If you want to have the months in the legend you will need to plot each value as a series rather than a data point in a single series.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: graphing a chart in VBA, showing series legends on the chart

    thanks and it worked.

    It does take me some time to read through lines of code to understand

    Do While .SeriesCollection.Count > 0
             'remove excess series
            .SeriesCollection(.SeriesCollection.Count).Delete
        Loop
    what does this code block do? If I got rid of it, nothing was changed. Is it normal?

    Thanks

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: graphing a chart in VBA, showing series legends on the chart

    If the activecell is in a block of data the add chart will attempt to use that data in the chart. The loop removes any series added by this action.
    Cheers
    Andy
    www.andypope.info

  6. #6
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: graphing a chart in VBA, showing series legends on the chart

    when you say the activecell is in a block of data, what do you mean by a block of data? You mean if those fiscal months are in all in one cell?

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: graphing a chart in VBA, showing series legends on the chart

    I mean if the activecell contains data then excel will try and use that cell and the adjacent cells to populate the chart. If the cell is empty the chart will be empty.

    Try it. Select an empty cell and press F11 to create a chart.
    Now select a cell that has data and press F11.
    Cheers
    Andy
    www.andypope.info

  8. #8
    Registered User
    Join Date
    03-05-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: graphing a chart in VBA, showing series legends on the chart

    how to draw graph i have no value
    can any one suggest approx. value and the graph like above

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0