+ Reply to Thread
Results 1 to 5 of 5

Thread: Chart source data - VBA

  1. #1
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    109

    Chart source data - VBA

    Hi,

    How do I define a chart's source data in VBA? Is it possible? I can't seem to find the command to do so

    Thanks
    Last edited by Coaster; 05-08-2010 at 04:58 PM.

  2. #2
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,151

    Re: Chart source data - VBA

    Coaster,

    start the macro recorder, select some data, create a chart. Select a chart series, change the data source. Stop the macro recorder. Study the code.

    This is for Excel 2003. If you use 2007 that won't work.

    Please update your profile and indicate your Excel version to take the guesswork out of the answering.

  3. #3
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: Chart source data - VBA

    I'm sorry I didn't remember that "little" detail lol

    Excel 2007...I tried doing that but didn't get anywhere, as you said...what should I do?

  4. #4
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,151

    Re: Chart source data - VBA

    Coaster, unfortunately the Excel 2007 macro recorder ignores a lot of things to do with charting. It gets better again with Excel 2010.

    I recorded the code below with 2010
    - selected a range, first column has dates, third and second column has numbers
    - created a chart (clustered column chart)
    - edited each series and...
    - defined new data ranges and data series title for two series
    - defined new range for X axis (categories)
    Hope you can glean something from that.

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
        Range("B11:D20").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlColumnClustered
        ActiveChart.SetSourceData Source:=Range("Sheet1!$B$11:$D$20")
        ActiveChart.SeriesCollection(1).Values = "=Sheet1!$C$2:$C$5"
        ActiveChart.SeriesCollection(1).Name = "=Sheet1!$C$1"
        ActiveChart.SeriesCollection(2).Values = "=Sheet1!$D$2:$D$5"
        ActiveChart.SeriesCollection(2).Name = "=Sheet1!$D$1"
        ActiveChart.SeriesCollection(2).XValues = "=Sheet1!$B$2:$B$5"
    End Sub
    cheers

  5. #5
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: Chart source data - VBA

    Thank you very much, that's just what i needed!

+ 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