I have created a macro that creates a scatter chart and series and then fills in the series with a shape. The first time I run the macro it creates the chart and plots the series but also plots a number of other points (almost looks like points at top of a bar chart). All subsequent times I run the macro after the first time the chart and fill plot just fine (add worksheets). Its only the first time that the plot is a mess. I don't know why this is. Any help would be appreciated. Thanks.bar
[CODESub Chart()
'Create a new chart.
Dim chtChart As Chart
Set chtChart = Charts.Add
With chtChart
'Set its properties
.Location Where:=xlLocationAsNewSheet
.ChartType = xlXYScatter
'Add Data
With .SeriesCollection.NewSeries
.Values = Array(5, 15, 35, 25, 10, 5, 5)
.XValues = Array(0.5, 2.4, 3.5, 3.6, 2.5, 0.5, 0.5)
End With
End With
'Fill Diagram with Color
Dim myCht As Chart
Dim mySrs As Series
Dim Npts As Integer, Ipts As Integer
Dim myBuilder As FreeformBuilder
Dim myShape As Shape
Dim Xnode As Double, Ynode As Double
Dim Xmin As Double, Xmax As Double
Dim Ymin As Double, Ymax As Double
Dim Xleft As Double, Ytop As Double
Dim Xwidth As Double, Yheight As Double
Set myCht = ActiveChart
Xleft = myCht.PlotArea.InsideLeft
Xwidth = myCht.PlotArea.InsideWidth
Ytop = myCht.PlotArea.InsideTop
Yheight = myCht.PlotArea.InsideHeight
Xmin = myCht.Axes(1).MinimumScale
Xmax = myCht.Axes(1).MaximumScale
Ymin = myCht.Axes(2).MinimumScale
Ymax = myCht.Axes(2).MaximumScale
Set mySrs = myCht.SeriesCollection(1)
Npts = mySrs.Points.Count
' First point
Xnode = Xleft + (mySrs.XValues(Npts) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = (Ytop + (Ymax - mySrs.Values(Npts)) * Yheight / (Ymax - Ymin))
Set myBuilder = myCht.Shapes.BuildFreeform(msoEditingAuto, Xnode, Ynode)
' Remaining points
For Ipts = 1 To Npts
Xnode = Xleft + (mySrs.XValues(Ipts) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = (Ytop + (Ymax - mySrs.Values(Ipts)) * Yheight / (Ymax - Ymin))
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode
Next Ipts
Set myShape = myBuilder.ConvertToShape
With myShape
' Fill with color
.Fill.ForeColor.SchemeColor = 53 ' Orange Fill
.Line.ForeColor.SchemeColor = 0 ' Black Outline
.Line.Weight = 0.25
End With
End Sub
][/CODE]
[CODESub Chart()
'Create a new chart.
Dim chtChart As Chart
Set chtChart = Charts.Add
With chtChart
'Set its properties
.Location Where:=xlLocationAsNewSheet
.ChartType = xlXYScatter
'Add Data
With .SeriesCollection.NewSeries
.Values = Array(5, 15, 35, 25, 10, 5, 5)
.XValues = Array(0.5, 2.4, 3.5, 3.6, 2.5, 0.5, 0.5)
End With
End With
'Fill Diagram with Color
Dim myCht As Chart
Dim mySrs As Series
Dim Npts As Integer, Ipts As Integer
Dim myBuilder As FreeformBuilder
Dim myShape As Shape
Dim Xnode As Double, Ynode As Double
Dim Xmin As Double, Xmax As Double
Dim Ymin As Double, Ymax As Double
Dim Xleft As Double, Ytop As Double
Dim Xwidth As Double, Yheight As Double
Set myCht = ActiveChart
Xleft = myCht.PlotArea.InsideLeft
Xwidth = myCht.PlotArea.InsideWidth
Ytop = myCht.PlotArea.InsideTop
Yheight = myCht.PlotArea.InsideHeight
Xmin = myCht.Axes(1).MinimumScale
Xmax = myCht.Axes(1).MaximumScale
Ymin = myCht.Axes(2).MinimumScale
Ymax = myCht.Axes(2).MaximumScale
Set mySrs = myCht.SeriesCollection(1)
Npts = mySrs.Points.Count
' First point
Xnode = Xleft + (mySrs.XValues(Npts) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = (Ytop + (Ymax - mySrs.Values(Npts)) * Yheight / (Ymax - Ymin))
Set myBuilder = myCht.Shapes.BuildFreeform(msoEditingAuto, Xnode, Ynode)
' Remaining points
For Ipts = 1 To Npts
Xnode = Xleft + (mySrs.XValues(Ipts) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = (Ytop + (Ymax - mySrs.Values(Ipts)) * Yheight / (Ymax - Ymin))
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode
Next Ipts
Set myShape = myBuilder.ConvertToShape
With myShape
' Fill with color
.Fill.ForeColor.SchemeColor = 53 ' Orange Fill
.Line.ForeColor.SchemeColor = 0 ' Black Outline
.Line.Weight = 0.25
End With
End Sub
][/CODE]