I have a macro that I've been working on for months and I can't get it to work.
I have gotten it to work with the first series, but only the first series in the chart are drawn on the chart.
Here's how the chart looks:
Here's the code to draw the chart (Excel 2010):
Code addapted from example at http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html
I have gotten it to work with the first series, but only the first series in the chart are drawn on the chart.
Here's how the chart looks:
Here's the code to draw the chart (Excel 2010):
Code:
Sub EmbeddedChartFromScratch(wbM As Workbook, blad As String)
Dim myChtObj As ChartObject
Dim rngChtData As Range
Dim rngChtXVal As Range
Dim iColumn As Long
' Using the selected range as the chart's data source.
' The first row contains the series labels, the first column contains the X values,
' and the rest of the columns contain the Y values for each series.
' make sure a range is selected
If TypeName(Selection) <> "Range" Then Exit Sub
' define chart data
Set rngChtData = Selection
' define chart's X values
With rngChtData
Set rngChtXVal = .Columns(1).Offset(1).Resize(.Rows.Count - 1)
End With
' add the chart
Set myChtObj = ActiveSheet.ChartObjects.Add(Left:=150, Width:=300, Top:=15, Height:=300)
With myChtObj.Chart
' make an line chart
.ChartType = xlLine
' remove extra series
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
' add series from selected range, column by column
For iColumn = 2 To rngChtData.Columns.Count
With .SeriesCollection.NewSeries
.Values = rngChtXVal.Offset(, iColumn - 1)
.XValues = rngChtXVal
.Name = rngChtData(1, iColumn)
End With
Next
End With
End Sub
Code addapted from example at http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html
Last edited: