VBA creating xy scatter plot with loop

StianKvam

New Member
Joined
Feb 23, 2018
Messages
4
Hi.

I have tried to build a code for creating an xy scatterplot. I have two columns where the values that I want to plot starts in cells W10 and AC10. The last cell used in the plot may differ from time to time based on manipulation in other parts of the worksheet.

For the values in column AC and W, I want to split it up into three series, series 1 if T >= 51, series 2 if 51 =< T >= 44 and series 3 if 44 =< T >= 17. These three series makes one graph combined.

Here's what I have so far:

Code:
Sub Reactor_temp()

    Dim embeddedchart As Chart
    Dim xrange As Range
    Dim yrange As Range
    Dim i As Long
    Dim T1 As Long
    
    T1 = 51 'Tank temperature Series 1
    'T2 = 44 ' Tank temperature series 2
    'T3 = 17 'Tank temperature series 3
    
    i = 9
             
    Set embeddedchart = ActiveSheet.Shapes.addChart.Chart
        
    With embeddedchart
        
        Do
        
        i = i + 1
        
            Set xrange = Sheets("Reactor Cryst.").Range("AC" & i)
            Set yrange = Sheets("Reactor Cryst.").Range("W" & i)
            .ChartType = xlXYScatterSmooth
            .SeriesCollection.NewSeries
            .SeriesCollection(1).XValues = xrange
            .SeriesCollection(1).Values = yrange
            
        Loop While Range("AC" & i) > T1 And i < 200
    
    End With
        
End Sub

When I run this code, the plot appears but only with the last datapoint, where T1 = 51 (cell AC28). The datapoint from cell AC10 to AC27 is not plotted.

I guess you need an if statement but I don't really know how to proceed.
As you can see from the code, I used a Do loop because I tried to make it work for the first series.
any help would be much appreciated!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top