XY Scatter graph

dboone25

Board Regular
Joined
May 8, 2015
Messages
185
Scatter graph learning curve.

Hello all. I am trying a simple method of creating a scatter graph using the code below. This is using the macro recording but eventually I would like to apply this to capture data on multiple sheets and creating multiple graphs on the same page, but for now this is to get a better understanding.

I would like to create a scatter graph where the range in each column can either finish at lets say A30 or A45 as the data set can change...how would I capture the 'unknown range in each column...e.g..


DataXY
1115132
1541584
12544588883
4584863
45445848453
488245448
856475458
588815487
8784969
68998765

<tbody>
</tbody>


The code I have below:

HTML:
Sub CommandButton_Click()

With ActiveSheet.ChartObjects.Add(Left:=100,Width:=375,Top:=75, Height:=225)
.chart.SetSourceData Source:=Sheets("Sheets2").range("A2:A10").range("B2:B10")
.chart.ChartType = xlXYScatterLines
End With

End Sub


Not sure if that makes sense but the code above does not work.

Basically I would like something dynamic as the range of data will be unknown. The range of data will have to be ( Data/X) and ( Data /Y )..

Am I going in the right direction? Please may someone help.
 
Re: XY Scatter graph help

This is fantastic, many many thanks for this.

Im just trying to tweek a few things and can see how to reposition the GAP by changing the integer.

Would you be able to tell me how to reposition each individual chart from left to right and how to name the series using a cell?

The naming of the series would be for:

Chart 1 : Series 1 ("E41") and Series 2 ("J40")

Chart 2 : Series 1 ("O41") and Series 2 ("T40")

Chart 3 : Series 1 ("Y41") and Series 2 ("AD40")

Chart 4 : Series 1 ("AI41") and Series 2 ("AN40")


Many many thanks for your help with this, very much appreciated.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Re: XY Scatter graph help

The following code has been amended to include the naming of each series for each chart...

Code:
[COLOR=darkblue]Sub[/COLOR] CommandButton_Click()

    [COLOR=darkblue]Dim[/COLOR] rStartCell [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rXVals [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rYVals [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] LeftPos [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]Const[/COLOR] GAP [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR] = 30 [COLOR=green]'gap between charts[/COLOR]
    
    LeftPos = 100 [COLOR=green]'starting left position of first chart[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet2")
        [COLOR=darkblue]Set[/COLOR] rStartCell = .Range("C41")
        [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 4
            LastRow = .Cells(.Rows.Count, rStartCell.Column).End(xlUp).Row
            [COLOR=darkblue]If[/COLOR] LastRow >= 41 [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]Set[/COLOR] rXVals = Range(rStartCell, .Cells(LastRow, rStartCell.Column))
                [COLOR=darkblue]Set[/COLOR] rYVals = rXVals.Offset(, 6).Resize(, 2)
                [COLOR=darkblue]With[/COLOR] ActiveSheet.ChartObjects.Add(Left:=LeftPos, Width:=375, Top:=75, Height:=225).Chart
                    .ChartType = xlXYScatterLines
                    [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] .SeriesCollection.Count > 0
                        .SeriesCollection(1).Delete
                    [COLOR=darkblue]Loop[/COLOR]
                    [COLOR=darkblue]For[/COLOR] j = 1 [COLOR=darkblue]To[/COLOR] rYVals.Columns.Count
                        [COLOR=darkblue]With[/COLOR] .SeriesCollection.NewSeries
                            [COLOR=darkblue]If[/COLOR] j = 1 [COLOR=darkblue]Then[/COLOR]
                                .Name = "=" & rStartCell.Offset(, 2).Address(, , , [COLOR=darkblue]True[/COLOR])
                            [COLOR=darkblue]Else[/COLOR]
                                .Name = "=" & rStartCell.Offset(-1, 7).Address(, , , [COLOR=darkblue]True[/COLOR])
                            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                    [COLOR=darkblue]Next[/COLOR] j
                    LeftPos = LeftPos + .Parent.Width + GAP
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]Set[/COLOR] rStartCell = rStartCell.Offset(, 10)
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Re: XY Scatter graph help

Brilliant work, thank you so much!!

When you use
Code:
LeftPos = LeftPos + .Parent.Width + GAP
this moves the position of the predefined values for the first graph.

Is there a way to individually move each graph a it would look very neat and tidy if each graph is placed under each dataset...is there a way to control the position of each individual graph?


Many thanks :)
 
Upvote 0
Re: XY Scatter graph help

The following code will place each chart 2 rows below their respective source data, and each will span the width of their respective columns (ie. C-J, M-T, etc) . . .

Code:
[COLOR=darkblue]Sub[/COLOR] CommandButton_Click()

    [COLOR=darkblue]Dim[/COLOR] rStartCell [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rXVals [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rYVals [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet2")
        [COLOR=darkblue]Set[/COLOR] rStartCell = .Range("C41")
        [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 4
            LastRow = .Cells(.Rows.Count, rStartCell.Column).End(xlUp).Row
            [COLOR=darkblue]If[/COLOR] LastRow >= 41 [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]Set[/COLOR] rXVals = Range(rStartCell, .Cells(LastRow, rStartCell.Column))
                [COLOR=darkblue]Set[/COLOR] rYVals = rXVals.Offset(, 6).Resize(, 2)
                [COLOR=darkblue]With[/COLOR] .ChartObjects.Add(Left:=rStartCell.Left, Width:=rStartCell.Resize(, 8).Width, Top:=.Cells(LastRow + 2, rStartCell.Column).Top, Height:=225).Chart
                    .ChartType = xlXYScatterLines
                    [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] .SeriesCollection.Count > 0
                        .SeriesCollection(1).Delete
                    [COLOR=darkblue]Loop[/COLOR]
                    [COLOR=darkblue]For[/COLOR] j = 1 [COLOR=darkblue]To[/COLOR] rYVals.Columns.Count
                        [COLOR=darkblue]With[/COLOR] .SeriesCollection.NewSeries
                            [COLOR=darkblue]If[/COLOR] j = 1 [COLOR=darkblue]Then[/COLOR]
                                .Name = "=" & rStartCell.Offset(, 2).Address(, , , [COLOR=darkblue]True[/COLOR])
                            [COLOR=darkblue]Else[/COLOR]
                                .Name = "=" & rStartCell.Offset(-1, 7).Address(, , , [COLOR=darkblue]True[/COLOR])
                            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                    [COLOR=darkblue]Next[/COLOR] j
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]Set[/COLOR] rStartCell = rStartCell.Offset(, 10)
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Re: XY Scatter graph help

Thank you for the updated code...little confused again on this one as when I run it I cant see any graphs being populated....

It would be grand if the graphs could be placed above each data set as in:

Graph 1 Size:- (B2 to Wide H2) and (B39 to H39 Long)

Graph 2 Size:- (L2 to Wide R2) and (L39 to R39 Long)

Graph 3 Size:- (V2 to Wide AB2) and (V39 to AB39 Long)

Graph 4 Size:- (AF2 to Wide AL2) and (AF39 to AL39 Long)


Hope that makes sense....
 
Upvote 0
Re: XY Scatter graph help

Thank you for the updated code...little confused again on this one as when I run it I cant see any graphs being populated....

I'm not sure why you would be confused. You said that you wanted the charts to be placed under each data set. So, since your data is in Sheet2, the charts should be in Sheet2 under each data set.

It would be grand if the graphs could be placed above each data set as in:

Graph 1 Size:- (B2 to Wide H2) and (B39 to H39 Long)

Graph 2 Size:- (L2 to Wide R2) and (L39 to R39 Long)

Graph 3 Size:- (V2 to Wide AB2) and (V39 to AB39 Long)

Graph 4 Size:- (AF2 to Wide AL2) and (AF39 to AL39 Long)

Try...

Code:
[COLOR=darkblue]Sub[/COLOR] CommandButton_Click()

    [COLOR=darkblue]Dim[/COLOR] rChartStartCell [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rDataStartCell [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rXVals [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rYVals [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet2")
        [COLOR=darkblue]Set[/COLOR] rChartStartCell = .Range("B2")
        [COLOR=darkblue]Set[/COLOR] rDataStartCell = .Range("C41")
        [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 4
            LastRow = .Cells(.Rows.Count, rDataStartCell.Column).End(xlUp).Row
            [COLOR=darkblue]If[/COLOR] LastRow >= 41 [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]Set[/COLOR] rXVals = Range(rDataStartCell, .Cells(LastRow, rDataStartCell.Column))
                [COLOR=darkblue]Set[/COLOR] rYVals = rXVals.Offset(, 6).Resize(, 2)
                [COLOR=darkblue]With[/COLOR] .ChartObjects.Add(Left:=rChartStartCell.Left, Width:=rChartStartCell.Resize(, 7).Width, Top:=rChartStartCell.[COLOR=darkblue]To[/COLOR]p, Height:=rChartStartCell.Resize(38).Height).Chart
                    .ChartType = xlXYScatterLines
                    [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] .SeriesCollection.Count > 0
                        .SeriesCollection(1).Delete
                    [COLOR=darkblue]Loop[/COLOR]
                    [COLOR=darkblue]For[/COLOR] j = 1 To rYVals.Columns.Count
                        [COLOR=darkblue]With[/COLOR] .SeriesCollection.NewSeries
                            [COLOR=darkblue]If[/COLOR] j = 1 [COLOR=darkblue]Then[/COLOR]
                                .Name = "=" & rDataStartCell.Offset(, 2).Address(, , , [COLOR=darkblue]True[/COLOR])
                            [COLOR=darkblue]Else[/COLOR]
                                .Name = "=" & rDataStartCell.Offset(-1, 7).Address(, , , [COLOR=darkblue]True[/COLOR])
                            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                    [COLOR=darkblue]Next[/COLOR] j
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]Set[/COLOR] rChartStartCell = rChartStartCell.Offset(, 10)
            [COLOR=darkblue]Set[/COLOR] rDataStartCell = rDataStartCell.Offset(, 10)
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Re: XY Scatter graph help

Brilliant! You are a genius! My apologies about the confusion.

Just changed a few things to line each individual graph but when I run a different data set the graphs dont seem to line up with the column reference.

Code:
'Graph 1  
    Dim rChartStartCell As range
    Dim rDataStartCell As range
    Dim rXVals As range
    Dim rYVals As range
    Dim LastRow As Long
    Dim i As Long
    Dim j As Long
    
    Dim LeftPos1 As Long
    Dim WidthPos1 As Long
    
    LeftPos1 = 35
    WidthPos1 = 620
    
    With Worksheets("Sheet2")
        Set rChartStartCell = .range("B2")
        Set rDataStartCell = .range("C41")
        For i = 1 To 1
            LastRow = .Cells(.Rows.count, rDataStartCell.Column).End(xlUp).Row
            If LastRow >= 41 Then
                Set rXVals = range(rDataStartCell, .Cells(LastRow, rDataStartCell.Column))
                Set rYVals = rXVals.Offset(, 6).Resize(, 2)
                With .ChartObjects.Add(Left:=LeftPos1, Width:=WidthPos1, Top:=rChartStartCell.Top, Height:=rChartStartCell.Resize(26).Height).chart
                    .ChartType = xlXYScatter
                    Do While .SeriesCollection.count > 0
                        .SeriesCollection(1).Delete
                    Loop
                    For j = 1 To rYVals.Columns.count
                        With .SeriesCollection.NewSeries
                            If j = 1 Then
                                .Name = "=" & rDataStartCell.Offset(, 2).Address(, , , True)
                            Else
                                .Name = "=" & rDataStartCell.Offset(-1, 7).Address(, , , True)
                            End If
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        End With
                    Next j
                End With
            End If
            Set rChartStartCell = rChartStartCell.Offset(, 8)
            Set rDataStartCell = rDataStartCell.Offset(, 10)
        Next i
    End With
  
  
  'Graph2
  Dim LeftPos2 As Long
  Dim WidthPos2 As Long
  
  LeftPos2 = 823
  WidthPos2 = 620
  
  With Worksheets("Sheet2")
        Set rChartStartCell = .range("L2")
        Set rDataStartCell = .range("M41")
        For i = 1 To 1
            LastRow = .Cells(.Rows.count, rDataStartCell.Column).End(xlUp).Row
            If LastRow >= 41 Then
                Set rXVals = range(rDataStartCell, .Cells(LastRow, rDataStartCell.Column))
                Set rYVals = rXVals.Offset(, 6).Resize(, 2)
                With .ChartObjects.Add(Left:=LeftPos2, Width:=WidthPos2, Top:=rChartStartCell.Top, Height:=rChartStartCell.Resize(26).Height).chart
                    .ChartType = xlXYScatter
                    Do While .SeriesCollection.count > 0
                        .SeriesCollection(1).Delete
                    Loop
                    For j = 1 To rYVals.Columns.count
                        With .SeriesCollection.NewSeries
                            If j = 1 Then
                                .Name = "=" & rDataStartCell.Offset(, 2).Address(, , , True)
                            Else
                                .Name = "=" & rDataStartCell.Offset(-1, 7).Address(, , , True)
                            End If
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        End With
                    Next j
                End With
            End If
            Set rChartStartCell = rChartStartCell.Offset(, 8)
            Set rDataStartCell = rDataStartCell.Offset(, 10)
        Next i
    End With
  
  'Graph 3
  Dim LeftPos3 As Long
  Dim WidthPos3 As Long
  
  LeftPos3 = 1608
  WidthPos3 = 620
  
  With Worksheets("Sheet2")
        Set rChartStartCell = .range("V2")
        Set rDataStartCell = .range("W41")
        For i = 1 To 1
            LastRow = .Cells(.Rows.count, rDataStartCell.Column).End(xlUp).Row
            If LastRow >= 41 Then
                Set rXVals = range(rDataStartCell, .Cells(LastRow, rDataStartCell.Column))
                Set rYVals = rXVals.Offset(, 6).Resize(, 2)
                With .ChartObjects.Add(Left:=LeftPos3, Width:=WidthPos3, Top:=rChartStartCell.Top, Height:=rChartStartCell.Resize(26).Height).chart
                    .ChartType = xlXYScatter
                    Do While .SeriesCollection.count > 0
                        .SeriesCollection(1).Delete
                    Loop
                    For j = 1 To rYVals.Columns.count
                        With .SeriesCollection.NewSeries
                            If j = 1 Then
                                .Name = "=" & rDataStartCell.Offset(, 2).Address(, , , True)
                            Else
                                .Name = "=" & rDataStartCell.Offset(-1, 7).Address(, , , True)
                            End If
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        End With
                    Next j
                End With
            End If
            Set rChartStartCell = rChartStartCell.Offset(, 8)
            Set rDataStartCell = rDataStartCell.Offset(, 10)
        Next i
    End With
  
  'Graph 4
  Dim LeftPos4 As Long
  Dim WidthPos4 As Long
  
  LeftPos4 = 2470
  WidthPos4 = 620
  
  With Worksheets("Sheet2")
        Set rChartStartCell = .range("AZ2")
        Set rDataStartCell = .range("AG41")
        For i = 1 To 1
            LastRow = .Cells(.Rows.count, rDataStartCell.Column).End(xlUp).Row
            If LastRow >= 41 Then
                Set rXVals = range(rDataStartCell, .Cells(LastRow, rDataStartCell.Column))
                Set rYVals = rXVals.Offset(, 6).Resize(, 2)
                With .ChartObjects.Add(Left:=LeftPos4, Width:=WidthPos4, Top:=rChartStartCell.Top, Height:=rChartStartCell.Resize(26).Height).chart
                    .ChartType = xlXYScatter
                    Do While .SeriesCollection.count > 0
                        .SeriesCollection(1).Delete
                    Loop
                    For j = 1 To rYVals.Columns.count
                        With .SeriesCollection.NewSeries
                            If j = 1 Then
                                .Name = "=" & rDataStartCell.Offset(, 2).Address(, , , True)
                            Else
                                .Name = "=" & rDataStartCell.Offset(-1, 7).Address(, , , True)
                            End If
                            .XValues = rXVals
                            .Values = rYVals.Columns(j)
                        End With
                    Next j
                End With
            End If
            Set rChartStartCell = rChartStartCell.Offset(, 8)
            Set rDataStartCell = rDataStartCell.Offset(, 10)
        Next i
    End With



To get each individual graph to start as per range i.e

Code:
Set rChartStartCell = .range("AZ2")

Unforutnately i cant get the graph to line up with that specific column and if I set each other one to a different location cell reference they wont move.

is it possible to do this?
 
Upvote 0
Re: XY Scatter graph help

If the size of each chart remains the same as previously, you only need to change these two lines...

Code:
        Set rChartStartCell = .range("B2")
        Set rDataStartCell = .range("C41")
 
Upvote 0
Re: XY Scatter graph help

Ahhh yes!! I have just realised something! Im so half asleep its unreal!

You are right! The graphs do start in those columns, but do to the length of the columns changing due to the column width changing with the size of the data each graph is pushed along and thats why it doesnt line up. For instance Graph 2 which is set to line up at ("L3), when i run the data set this is pushed along to column ("P).

I dont suppose these is a way to lock the graph to line up with those columns...perhpaps lock them into those positions?

Every data set is different, hence why the graphs move.

Hope I have explained that...sorry to be another pain in the neck.
 
Upvote 0
Re: XY Scatter graph help

Unclear, can you please be very specific in your instructions?
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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