Creating Excel chart using Vba showing wrong results

praveenpatel421983

New Member
Joined
Aug 17, 2017
Messages
41
Hi,

I am trying to create a excel line chart using vba using the information in the same sheet. For some reason result has 2 lines in the graph which is supposed to be 1 line. I am not sure what is going wrong in that. Could someone guide me what is going wrong? I have tried searching this forum but couldn't find the right post.

Set chrt2 = Sheets("Graphs").Shapes.AddChart.Chart
With chrt2
GraphName = Sheets("Graphs").Cells(34, 1).Value
.ChartArea.Left = 600
.ChartArea.Top = 690
.ChartArea.Height = 300
.ChartArea.Width = 1200
.ChartType = xlLine
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "=""Scatter Chart2"""
.SetSourceData Source:=Sheets("Graphs").Range("$A$35:$D" & LastColGraph), PlotBy:=xlColumns
.SeriesCollection(1).XValues = "=Graphs!$A$35:$A" & LastColGraph
.SeriesCollection(1).Values = "=Graphs!$D$35:$D" & LastColGraph
.HasTitle = True
.ChartTitle.Characters.Text = GraphName
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = GraphName
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = Sheets("Graphs").Cells(34, 4).Text
.Axes(xlCategory).HasMajorGridlines = False
.Axes(xlCategory).HasMinorGridlines = False
.Axes(xlValue).HasMajorGridlines = True
.Axes(xlValue).HasMinorGridlines = False
.HasLegend = False
.ChartArea.Interior.Color = RGB(245, 245, 245)
.PlotArea.Interior.Color = RGB(219, 229, 241)
.SeriesCollection(1).Border.Color = RGB(46, 108, 150)
End With

Thanks
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi

Didn't try but looking at the code I'd say you have not 2 but 3 lines, 2 of them superposed.

Try deleting:

Code:
.SetSourceData Source:=Sheets("Graphs").Range("$A$35:$D" & LastColGraph), PlotBy:=xlColumns
 
Upvote 0
Hi,

Oh, I didn't knew that. You are right!
I was using below code before, it used to have 2 lines.
Please let me know what do you recommend?

Code:
.SetSourceData Source:=Sheets("Graphs").Range("$A$35:$A" & LastColGraph, "$D$35:$D" & LastColGraph), PlotBy:=xlColumns
 
Upvote 0
You just have to delete the .SetSourceData line.

The rest of the code that you have already adds a series and defines the source data.

Code:
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "=""Scatter Chart2"""
.SeriesCollection(1).XValues = "=Graphs!$A$35:$A" & LastColGraph
.SeriesCollection(1).Values = "=Graphs!$D$35:$D" & LastColGraph
 
Last edited:
Upvote 0
Sorry for replying late.

I did the way you informed and it worked for me. You are awesome :) . Thanks a lot pgc01!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,927
Members
449,195
Latest member
Stevenciu

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