Results 1 to 4 of 4

Thread: How to create a correct chart using VBA?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    14

    How to create a correct chart using VBA?

    Hi,
    I used the following code trying to create a chart but the lines showing in the chart are sort of accumulation of the data below, which is not what I expected. They should be individually showing themselves instead of summing up.
    Is there any way to get what I expected? Please see the attachment but I'm not sure it's attached.
    Thanks in advance.

    Sub CreateChart()
    'On Error Resume Next
    Range("A3:N21").Select
    Charts.Add
    ActiveChart.ChartType = xlLineMarkersStacked
    ActiveChart.SetSourceData Source:=Sheets("GraphDisplayMKT").Range("C24:N30"), _
    PlotBy:=xlRows
    ActiveChart.Location Where:=xlLocationAsObject, Name:="GraphDisplayMKT"
    With ActiveChart
    .HasTitle = True
    .HasLegend = False
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "BRA Share"
    .ChartTitle.Characters.Text = "BRA Product"
    .HeightPercent = 60
    End With
    With ActiveSheet.Shapes("Chart 1")
    .ScaleHeight 1.39, msoFalse, msoScaleFromBottomRight
    .ScaleWidth 1.5, msoFalse, msoScaleFromBottomRight
    .ScaleHeight 0.77, msoFalse, msoScaleFromTopLeft
    .ScaleWidth 1.22, msoFalse, msoScaleFromTopLeft
    End With
    ActiveChart.ChartArea.Select
    With Selection.Font
    .Name = "Arial"
    .FontStyle = "Bold"
    .Size = 7
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    End With
    ActiveChart.Legend.Delete = True
    On Error GoTo 0
    End Sub

  2. #2

  3. #3
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: How to create a correct chart using VBA?

    If you want multiple lines on a graph, each line is referred to as a series... and you have to set the source of each of these series... Here is some code you can easily implement into your own...

    Sub AddNewSeries()
    With ActiveChart.SeriesCollection.NewSeries
    .Name = "My Series"
    .Values = ActiveSheet.Range("G4:G14")
    .XValues = ActiveSheet.Range("A4:A14")
    End With
    End Sub

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: How to create a correct chart using VBA?

    If you're going to set the chart type as xlLineMarkersStacked in the 5th line of your code, i.e. stacking the data up, then I don't know what else you'd expect.

    Try xlLineMarkers instead.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

Posting Permissions

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



Click Here to Expand Forum to Full Width