View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Greg Fish Greg Fish is offline
external usenet poster
 
Posts: 10
Default Using VBA to create and format Chart

Jon -

I was able to figure that out, prior to reading your post. Of course your
website was illuminating and pointed me in the right direction. I ran the
macro recorded for individual actions that I wanted to do, so as change the
line type. Anyhow here is the complete code for others to look at, should it
prove helpful. Likewise a critique would be useful, I am still in the
learning process, and consider myself to be a novice with regard to overall
VBA profeciency.

Sub graphIt(x1Axis As String, y1Axis As String, x_EST As String, y_EST As
String, curve As String, xLabel As String, yLabel As String)
Dim objWorkSheet As Object
Dim Y_data_range, X_data_range As Range
Dim Y_est_data_range, X_est_data_range As Range
Dim sY_data_range As String
Dim sX_data_range As String
Dim sY_est_data_range As String
Dim sX_est_data_range As String
Dim fittedCurve As String
Dim xAxisLabel As String
Dim yAxisLabel As String
Dim fuelCurve As ChartObject
Dim seriesData As Series
Dim seriesEst As Series
Worksheets(2).Activate
Set Y_data_range = ActiveSheet.Range(y1Axis)
Set X_data_range = ActiveSheet.Range(x1Axis)
Set Y_est_data_range = ActiveSheet.Range(y_EST)
Set X_est_data_range = ActiveSheet.Range(x_EST)
sY_data_range = "'" & ActiveSheet.Name & "'!" &
Y_data_range.Address(ReferenceStyle:=xlR1C1)
sX_data_range = "'" & ActiveSheet.Name & "'!" &
X_data_range.Address(ReferenceStyle:=xlR1C1)
sY_est_data_range = "'" & ActiveSheet.Name & "'!" &
Y_est_data_range.Address(ReferenceStyle:=xlR1C1)
sX_est_data_range = "'" & ActiveSheet.Name & "'!" &
X_est_data_range.Address(ReferenceStyle:=xlR1C1)
fittedCurve = curve
xAxisLabel = xLabel
yAxisLabel = yLabel

Worksheets(1).Activate
Set fuelCurve = ActiveSheet.ChartObjects.Add _
(Left:=150, Width:=750, Top:=25, Height:=450)
With fuelCurve.Chart

'make smoth XY Scatter Chart
.chartType = xlXYScatterSmooth
'.ChartTitle ' add string
'
' add series from selected name ranges

With .SeriesCollection.NewSeries
.Values = "=" & sY_data_range
.XValues = "=" & sX_data_range
.Name = "Data"
End With

With .SeriesCollection.NewSeries
.Values = "=" & sY_est_data_range
.XValues = "=" & sX_est_data_range
.Name = "Estimated"
End With
End With
With fuelCurve.Chart.Axes(xlCategory)
.HasTitle = True
.AxisTitle.Characters.Text = xAxisLabel
.HasMajorGridlines = True
.HasMinorGridlines = True
End With
With fuelCurve.Chart.Axes(xlValue)
.HasTitle = True
.AxisTitle.Characters.Text = yAxisLabel
.HasMajorGridlines = True
.HasMinorGridlines = True
End With
With fuelCurve.Chart.SeriesCollection(1)
With .Border
.Weight = xlThin
.LineStyle = xlNone
End With
If fittedCurve = "Exponent" Then
.Trendlines.Add Type:=xlExponential
Else
.Trendlines.Add Type:=xlPolynomial
End If
End With
fuelCurve.Chart.SeriesCollection(1).Trendlines(1). DisplayEquation = True
fuelCurve.Chart.PlotArea.Interior.ColorIndex = xlNone
End Sub