Results 1 to 3 of 3

XY Scatter Plot Data Series Additions within a loop

Threaded View

  1. #1
    Registered User
    Join Date
    08-09-2011
    Location
    Wilmington, DE
    MS-Off Ver
    Excel 2003
    Posts
    2

    XY Scatter Plot Data Series Additions within a loop

    Hi,

    I am trying to create an XY Scatter Plot within Excel 2003 that has 5 series of data on it. My data exists in two columns (A and B) on the spreadsheet. There is an unspecified number of rows of data within each data set, and each set of data is located 8 rows down from the end of the previous data set (all within columns A and B). Because the number of rows in each data set is variable, I can't simply create a loop that selects a pre-determined number of rows.

    To solve this issue, I created a Do While loop that uses the End(xlDown) function. The code I wrote to graph the data can properly plot one range when the code is located outside of the loop, but once placed inside the loop, running the maco simply creates a spreadsheet with no data plotted.

    My issue is with the data series creation - the data is not being properly placed on the chart created within the workbook. Does anyone see an error/mistake in this code?

    I am new to writing Excel Macros, so please forgive any seemingly simple errors I have made.

    Thank you for the help.

    My code is below, and I have attached the spreadsheet.

    Option Explicit
    
    Sub NewGraph()
        Dim cell1, cell2, cell3, cell4, rng1, rng2 As Range
        Dim i As Integer
        i = 1
        Set cell1 = Cells(1, 1)
        Set cell2 = cell1.End(xlDown)
        Set cell3 = Cells(1, 2)
        Set cell4 = cell3.End(xlDown)
        
        'Debugging Help
        On Error Resume Next
        If Err.Number <> 0 Then
            MsgBox Err.Description, vbCritical, "Error # " & Err.Number
        End If
        'End Debugging Help
    
        With Charts.Add ' Make new chart
        ' Set its properties
        .ChartType = xlXYScatter
        .Location Where:=xlLocationAsNewSheet
        .HasLegend = False
        End With
    
        Do While i <= 5
            Set rng1 = Range(cell1, cell2)
            Set rng2 = Range(cell3, cell4)
            'Insert series naming scheme here as well (always a certain distance from cell1)
            'Insert graphing code here
            
            ' Add the series
            With ActiveChart.SeriesCollection.NewSeries
                .XValues = Worksheets("Sheet1").rng1
                .Values = Worksheets("Sheet1").rng2
            End With
            
            Set cell1 = (cell2.End(xlDown).End(xlDown))
            Set cell2 = cell1.End(xlDown)
            Set cell3 = (cell4.End(xlDown).End(xlDown))
            Set cell4 = cell3.End(xlDown)
                    
            'rng2.Select
            i = i + 1
        Loop
         
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1