0

I have been working on an excel Macro to graph some data on Chartsheet, but all of a sudden, it now graphs 7 different series instead of 2...

The Code for the graph is:

Public Sub GraphResults()
Dim ws As Worksheet
Dim LineGraph As Chart
Set ws = ActiveSheet
Set LineGraph = Charts.Add

    With LineGraph
        .SetSourceData Source:=ws.Range("B29:B35,G29:G35")
        .ChartType = xlLineMarkers
        .HasTitle = True
        .ChartTitle.Text = ""
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X-axis"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y-axis"
        .SeriesCollection(1).XValues = ws.Range("A29:A35")
    End With


End Sub

Instead of graphing 2 lines where one line has Y-values of B29:B35 and the other has Y values of G29:G35, it graphs 7 lines

Each line has a B value and a G value. For example, series 1 contains 2 points: B29 and G29. Series 2 contains 2 points B30 and G30. I didn't change the code at all. In fact when I open the Macro from the last time it was saved, the graph that displays only has 2 lines! When I re-run the macro, the graph changes to 7 lines.

What's going on here?...

Community
  • 1
  • 1
Dives
  • 13
  • 3

1 Answers1

0

Your chart is plotting values by rows, not columns as you want. See SetSourceData Method for more info.

Add PlotBy:=xlColumns to the .SetSourceData line to automatically plot by columns.

BigBen
  • 46,229
  • 7
  • 24
  • 40