+ Reply to Thread
Results 1 to 4 of 4

Excel 2007 : VBA XY scatter plot problems

Hybrid View

  1. #1
    Registered User
    Join Date
    05-29-2009
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    2

    VBA XY scatter plot problems

    Hi all,

    I am new to VBA, and I'm using Excel 2007 to do some automated graphing for work. Anyhow, I am having heck of a time getting a macro I am working on to do what I want. Here's what's happening:

    1. I record the macro - I select both columns in the attached sheet - Go to insert chart- select the XY scatter plot - and it gives me a linear graph with the XY axis in the right place (A is X axis, B is Y axis). However, when I run this macro, it totally screws up the axis variables, and my graph comes out incorrect - I can eve tell where it's getting mixed up.

    I realize at this point the chart wizard would be appropriate, but I need automation to be incorporated within this sheet. I am writing this macro because I will need to append this graph with new curves as I bring more columns of data in, but I can't even get it to do a simple plot yet - Any ideas?

    Attached is the document

    Here is the code for reference:

    Sub Macro4()
    
        Columns("A:B").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Range("'500mW impedance'!$A:$B")
        ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    
    End Sub



    Thanks!

    Jordan
    Attached Files Attached Files
    Last edited by jordan_schultz; 05-30-2009 at 07:58 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA XY scatter plot problems

    Welcome to the forum.

    Please take a few minutes to read the forum rules, and then edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    05-29-2009
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: VBA XY scatter plot problems

    I added the above code tags - Is this what you were referring to? Sorry for the mess up.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA XY scatter plot problems

    I change the code slightly so it should be easier to adapt for future use.

    Sub Macro3()
    '
    ' Macro3 Macro
    '
        Dim rngXData As Range
        Dim rngYData As Range
    '
        Set rngXData = Range("A2", Cells(Rows.Count, 1).End(xlUp))
        Set rngYData = rngXData.Offset(0, 1)
        
        With ActiveSheet.Shapes.AddChart.Chart
            Do While .SeriesCollection.Count > 0
                ' remove any series added automatically
                .SeriesCollection(1).Delete
            Loop
            With .SeriesCollection.NewSeries
                .Values = rngYData
                .XValues = rngXData
                .Name = "='" & ActiveSheet.Name & "'!" & rngYData.Offset(-1, 0).Cells(1, 1).Address
            End With
            .ChartType = xlXYScatterSmoothNoMarkers
        End With
        
    End Sub
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

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