PDA

View Full Version : VBA Macro Creates Scatter Plot in 2003, Adds Lines in 2007???



kjb034
03-26-2009, 10:12 AM
I have the following macro. It works fine, creating an XY scatter plot in Excel 2003, but when one of my users runs it in Excel 2007, it creates a scatter plot with lines. Why does it do this? I am using "ChartType = xlXYScatter", not xlXYScatterLines.

Public Sub CreateChart(ByVal viLabeledSeries As Integer, ByVal viNonLabeledSeries As Integer, ByVal vsWorkbookName As String, ByVal vsPopulationName As String, ByVal vsPrimaryAxisTitle As String, ByVal vsSecondaryAxisTitle As String)
Dim i As Integer
Dim series As series
Dim sXColumn As String
Dim sYColumn As String

'Create the chart
Charts.Add
ActiveChart.ChartType = xlXYScatter
ActiveChart.PlotBy = xlColumns

For i = ActiveChart.SeriesCollection.Count To 1 Step -1
ActiveChart.SeriesCollection(i).Delete
Next i

'Add the data series
For i = 1 To viNonLabeledSeries
sXColumn = GetColumnNameFromNumber((i - 1) * 2 + 4)
sYColumn = GetColumnNameFromNumber((i - 1) * 2 + 5)
Set series = ActiveChart.SeriesCollection.NewSeries
series.XValues = Sheets("Data").Range(sXColumn & "1:" & sXColumn & "30000")
series.Values = Sheets("Data").Range(sYColumn & "1:" & sYColumn & "30000")
Next i

For i = 1 To viLabeledSeries
'Add the first data series because this way, the points will be on top of the other points so it's easier to see them
Set series = ActiveChart.SeriesCollection.NewSeries
series.Name = Sheets("Data").Range("A" & i)
series.XValues = Sheets("Data").Range("B" & i & ":B" & i)
series.Values = Sheets("Data").Range("C" & i & ":C" & i)
Next i


ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Chart"
ActiveChart.PlotArea.Select
Selection.ClearFormats