1 Attachment(s)
Creating a line chart in Excel VBA
Hi everyone,
I've asked about this before, and decided a pie chart wasnt the way to go. I need a chart to be dyamically created when the Command Button "Display Graph" is pressed, to show the values in the range of cells K12-N50 in the Pipeline sheet (not the totals, just the values themselve). Also, the legend should display the titles in cells K11-N11 in Pipeline.
For a while it was ok, then started doing something odd. Now its not displaying anything. Also I'd like something inside the code that if the chart that is created dynamically already exists, replace it with the updated graph (delete+replace?).
If anyone can help, heres the files! (the spreadsheet takes its values from the database)
Re: Creating a line chart in Excel VBA
Hi,
when you are adding your series, you can only add a single column or row.
you are trying to add a range of cells:
VB Code:
Dim TheSeries As Series
Newchart.SeriesCollection.Add _
Source:=Worksheets("Pipeline").Range("K$12:N$50")
you will need to add more than 1 series to your chart. e.g.
VB Code:
Newchart.SeriesCollection.Add _
Source:=Worksheets("Pipeline").Range("K$12:K$50")
Newchart.SeriesCollection.Add _
Source:=Worksheets("Pipeline").Range("L$12:L$50")
Newchart.SeriesCollection.Add _
Source:=Worksheets("Pipeline").Range("M$12:M$50")
Newchart.SeriesCollection.Add _
Source:=Worksheets("Pipeline").Range("N$12:N$50")
also, you are setting your x axis labels incorrectly. should be:
VB Code:
With NewChart.SeriesCollection(1) 'thsi index should ideally be an incremental count
.XValues = _
Worksheets("Pipeline").Range("B$12") 'this is the x axis labels
ideally you should be using variables for a count, and start cells, incrementing them within a loop to cover all series.
hope this gives you a starting point.