Consulting

Results 1 to 5 of 5

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

  1. #1

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

    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

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
        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

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    sorry, I am wrong.

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    "Next j " should be moved to the last line?

  5. #5
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •