PDA

View Full Version : Nested Loop to create multiple series in a single chart. Where did I go wrong??



davidmnix
01-18-2017, 09:04 PM
So I created the i loop and managed to get a single series to plot as its supposed to, but now I need to run the same function using different j values and add a new series to the chart for each j. How do I do that? Everything I've tried so far either creates 5 separate charts or doesn't run.

Any help is greatly appreciated. Below is a segment from my code. I removed all the attempts I had previously made at having the j loop create the series.



For j = 1 To 1.2 Step 0.05
Tr(j) = Tc * j
For i = 1 To 150
P(i) = i
Z(i) = PR_Z_Solve(Tr(j), Pc, w, P(i), V)
Next i
Next j
Dim Plot As Chart
Set Plot = ActiveWorkbook.Charts.Add
Dim Series As Series
Set Series = ActiveChart.SeriesCollection.NewSeries
With ActiveChart
With Series
.ChartType = xlXYScatterLinesNoMarkers
.XValues = P
.Values = Z
End With
With ActiveChart
.Name = TabName
.HasLegend = False
With .PlotArea.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
End With
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Z"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "P"
.Axes(xlCategory).MinimumScale = 1
.Axes(xlCategory).MaximumScale = 150
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 1
.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Times New Roman"
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 18
End With

mana
01-19-2017, 02:32 AM
Dim j As Double
Dim i As Long
Dim k As Long


For k = 0 To 4
j = 1 + k / 20
Tr(j) = Tc * j
For i = 1 To 150
P(i) = i
Z(i) = PR_Z_Solve(Tr(j), Pc, w, P(i), V)
Next i
Next k

mana
01-19-2017, 02:42 AM
sorry, I am wrong.

mana
01-19-2017, 02:52 AM
"Next j " should be moved to the last line?

mana
01-19-2017, 03:06 AM
With ActiveChart
.ChartType = xlXYScatterLinesNoMarkers

For j = 1 To 1.2 Step 0.05
Tr(j) = Tc * j
For i = 1 To 150
P(i) = i
Z(i) = PR_Z_Solve(Tr(j), Pc, w, P(i), V)
Next i
With .SeriesCollection.NewSeries
.XValues = P
.Values = Z
End With
Next j

.Name = TabName
.HasLegend = False
With .PlotArea.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
End With
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Z"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "P"
.Axes(xlCategory).MinimumScale = 1
.Axes(xlCategory).MaximumScale = 150
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 1
.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Times New Roman"
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 18
End With