+ Reply to Thread
Results 1 to 11 of 11

Thread: Adding additional source data to a chart

  1. #1
    Registered User
    Join Date
    10-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Adding additional source data to a chart

    This is the first time I'm using Macros in Excel and I'm having trouble trying to add additional source data to a chart. I created a macro using the record macro tool; however it only generates a new series on the chart. The Name, X Value and Y Value fields are completely empty. How do I change the macro to populate them?

    Thanks

    
    'Sub add_data()
    '
    ' add_data Macro
    ' Macro recorded 18/10/2011 by '
        ActiveChart.SeriesCollection(5).XValues = "=BER!R26C2:R33C2"
        ActiveChart.SeriesCollection(5).Values = "=BER!R26C7:R33C7"
        ActiveChart.SeriesCollection(5).Name = "=BER!R20C3:R20C7"
    End Sub

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: Adding additional source data to a chart

    Try

    ActiveChart.SeriesCollection.NewSeries
    and then set the values in a similar way to those in the code that you recorded.
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Registered User
    Join Date
    10-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Adding additional source data to a chart

    Thanks. I tried that and it still creates a new series without data. A run-time error occurs, when going into the debug option the second line in code is highlighted. I'm very new to all this and really appreciate you helping me out :-)

    Code:
    sub snb()
    ActiveChart.SeriesCollection(5).XValues = "=BER!R26C2:R33C2
    end sub

  4. #4
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: Adding additional source data to a chart

    Try this.

    Sub TEst()
    ActiveChart.SeriesCollection(5).XValues = "=BER!R26C2:R33C2"
    ActiveChart.SeriesCollection(5).Values = "=BER!R26C7:R33C7"
    ActiveChart.SeriesCollection(5).Name = "=BER!R20C3:R20C7"
    
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(6).XValues = "=BER!R26C2:R33C2"
    ActiveChart.SeriesCollection(6).Values = "=BER!R26C7:R33C7"
    ActiveChart.SeriesCollection(6).Name = "=BER!R20C3:R20C7"
    'Adjust the values for the new series to the correct ranges
    
    End Sub
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  5. #5
    Registered User
    Join Date
    10-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Adding additional source data to a chart

    That also still doesnt work :-( this time the debugger higlights the .values line. I'm starting to think it might be easier to keep adding measurements manualy :-(

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Adding additional source data to a chart

    It would probably help to post example file explain further what the code should do.
    Cheers
    Andy
    www.andypope.info

  7. #7
    Registered User
    Join Date
    10-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Adding additional source data to a chart

    I've (hopefully) attatched a simpler version of what I'm attempting to do.

    Basically I have three sets of results for one mission. In order to easily compare new missions to older ones I want to plot the data onto a graph. However I currently have over 20 old missions with new ones coming every six months or so. And as I'm not the only person who will be using this document in the future I wanted to automate all the graphing functions. So in the future they will simply copy the data input tabel then implement a macro for it to add the data to the chart.

    I thought I'd try to get a simple macro to just plot data before I attempted to work out how to make it shift down the sheet as new data sets are added.

    Thanks for trying to help me solve this problem :-)

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Adding additional source data to a chart

    No attachment. Is it too big?
    Cheers
    Andy
    www.andypope.info

  9. #9
    Registered User
    Join Date
    10-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Adding additional source data to a chart

    I dont think so I completly striped it down its only 36.5KB. It says its attatched in the Attatch files section.
    Attached Files Attached Files

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Adding additional source data to a chart

    This will prompt the use for a test name.
    It will use series 4 to 6, creating series if required.

    Sub Add_data()
    '
    ' Add_data Macro
    ' Macro recorded 20/10/2011 by lfoote
    '
        Dim strTest As String
        Dim lngRow As Long
        Dim rngDataX As Range
        Dim rngDataY As Range
        Dim rngName As Range
        Dim lngIndex As Long
        Dim objSeries As Series
        
    '
        strTest = InputBox("Enter Number of test to compare to Test 1", , "Test 2")
        Set rngfind = Sheet1.Range("C:E").Find(strTest)
        If Not rngfind Is Nothing Then
            lngRow = rngfind.Row
            
            Set rngDataX = Sheet1.Cells(lngRow + 6, 2).Resize(8, 1)
            Set rngDataY = rngDataX.Offset(, 5)
            Set rngName = rngfind.Resize(1, 5)
            For lngIndex = 4 To 6
                With Charts("Chart1")
                    If lngIndex <= .SeriesCollection.Count Then
                        Set objSeries = .SeriesCollection(lngIndex)
                    Else
                        Set objSeries = .SeriesCollection.NewSeries
                    End If
                End With
                
                objSeries.Values = rngDataY
                objSeries.XValues = rngDataX
                objSeries.Name = "='" & rngName.Parent.Name & "'!" & rngName.Address(, , xlR1C1)
                
                Set rngDataX = rngDataX.Offset(0, 7)
                Set rngDataY = rngDataY.Offset(0, 7)
                Set rngName = rngName.Offset(0, 7)
            Next
        Else
            MsgBox "Could not locate " & strtext, vbExclamation
        End If
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  11. #11
    Registered User
    Join Date
    10-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Adding additional source data to a chart

    Thank you so much its perfect! :-D

+ 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.2.0