Scatter plot based on selection

carter963

New Member
Joined
Mar 16, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I wanted to automate making a scatter plot based on a selection. I snagged some code from here and tried to modify it to work with my data. I though I understood the code, but it's giving me a lot of trouble mostly around how each series is added. I finally got it to select based on rows, but it's adding too many series for each sample. The original code had everything in columns, but typically we get the data from vendors in the format in the table below. It would be more difficult to get people to change how they arrange the data than just changing the code. I also want to understand what's going on with the code... any recommendations on resources for this topic would be helpful.

Time510152030456075
Sample194.8106.5106.3105.7106.3105.8105.8105.8
Sample293.3101.2101.4101.2101.9101.7102.7102.4
Sample3100.0100.8100.6100.4100.1100.5100.2100.8
Sample496.7101.7102.1102.2102.3102.3102.4102.1
Sample59.924.137.548.165.182.893.5101.0
Sample647.797.2104.5104.7104.7104.2104.8104.8


VBA Code:
Option Explicit

Sub MultiY_OneX_Chart()

    Dim rngDataSource As Range
    Dim iDataRowsCt As Long
    Dim iDataColsCt As Integer
    Dim iSrsIx As Integer
    Dim iSrsIy As Integer
    Dim chtChart As Chart
    Dim srsNew As Series

    If Not TypeName(Selection) = "Range" Then
        '' Doesn't work if no range is selected
        MsgBox "Please select a data range and try again.", _
            vbExclamation, "No Range Selected"
    Else
        Set rngDataSource = Selection
        With rngDataSource
            iDataRowsCt = .Rows.Count
            iDataColsCt = .Columns.Count
        End With

        '' Create the chart
        Set chtChart = ActiveSheet.ChartObjects.Add( _
            Left:=ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left + _
                ActiveWindow.Width / 4, _
            Width:=ActiveWindow.Width / 2, _
            Top:=ActiveSheet.Rows(ActiveWindow.ScrollRow).Top + _
                ActiveWindow.Height / 4, _
            Height:=ActiveWindow.Height / 2).Chart

        With chtChart
            .ChartType = xlXYScatterLines

            '' Remove any series created with the chart
            Do Until .SeriesCollection.Count = 0
                .SeriesCollection(1).Delete
            Loop

            For iSrsIx = 1 To iDataRowsCt - 1
            For iSrsIy = 1 To iDataColsCt - 1
                    
                '' Add each series
                Set srsNew = .SeriesCollection.NewSeries
                With srsNew
                    .Name = rngDataSource.Cells(iSrsIx + 1, 1)
                    .Values = rngDataSource.Cells(iSrsIy + 1, 2) _
                        .Resize(1, iDataColsCt - 1)
                    .XValues = rngDataSource.Cells(1, 2) _
                        .Resize(1, iDataColsCt - 1)
                 
                End With
             Next
             Next
        End With
    End If
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
It's an old post, but you're probably still somewhat interested. And that code of mine is much older!

You don't need nested loops, just For iSrsIx = 1 To iDataRowsCt - 1. iSrsIx means Series Index, not something in the x direction, and nothing is needed in the y direction.

Here's the updated code.

VBA Code:
Sub MultiY_OneX_Chart()
  
  If Not TypeName(Selection) = "Range" Then
    '' Doesn't work if no range is selected
    MsgBox "Please select a data range and try again.", _
      vbExclamation, "No Range Selected"
  Else
    Dim rngDataSource As Range
    Set rngDataSource = Selection
    With rngDataSource
      Dim iDataRowsCt As Long, iDataColsCt As Long
      iDataRowsCt = .Rows.Count
      iDataColsCt = .Columns.Count
    End With

    '' Create the chart
    Dim chtChart As Chart
    Set chtChart = ActiveSheet.Shapes.AddChart2(, xlXYScatterLines).Chart

    With chtChart
      '' Remove any series created with the chart
      Do Until .SeriesCollection.Count = 0
        .SeriesCollection(1).Delete
      Loop

      Dim iSrsIx As Long
      For iSrsIx = 1 To iDataRowsCt - 1
          '' Add each series
          Dim srsNew As Series
          Set srsNew = .SeriesCollection.NewSeries
          With srsNew
            .Name = rngDataSource.Cells(iSrsIx + 1, 1)
            .Values = rngDataSource.Cells(iSrsIx + 1, 2) _
              .Resize(1, iDataColsCt - 1)
            .XValues = rngDataSource.Cells(1, 2) _
              .Resize(1, iDataColsCt - 1)
          End With
      Next
    End With
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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