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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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