+ Reply to Thread
Results 1 to 4 of 4

Macro to change new chart's source data to reference cells.

  1. #1
    Registered User
    Join Date
    11-27-2012
    Location
    Houston
    MS-Off Ver
    Excel 2007
    Posts
    3

    Macro to change new chart's source data to reference cells.

    Hello All,

    Let me start by saying that I don't think a dynamically ranged chart will work for what I want. Myself and others will be making many more graphs as time goes on, and you'd have to go in and set up each and every new chart separately with the naming and whatnot.

    I'm trying to write a macro that refers to two cells at the top of the worksheet to update a graph's source data. The columns for each series will not change, only the rows. I've copied & pasted a chart. Now, for the newly pasted chart, I want to change the source data, but just the rows in each of the series.

    In cell A1, I have the value 1001.
    In cell A2, I have the value 2000.

    Here's the code I've recorded manually:

    Please Login or Register  to view this content.
    Can I reproduce the result of the above code by referencing the values in cells A1 and A2? My idea looks like this (but doesn't work):

    Please Login or Register  to view this content.
    Thanks for any tips thrown my way!!
    Last edited by jrod20; 11-28-2012 at 11:46 AM. Reason: code tags

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Macro to change new chart's source data to reference cells.

    Hi
    have you tried using offset and referring to the values in cells A1 and A2,
    something like

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-27-2012
    Location
    Houston
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Macro to change new chart's source data to reference cells.

    NickyC,
    Thanks for the quick reply!

    I think OFFSET will work, but I need a boost since I'm a newb at VBA. Here's what I tried (note that I've changed the name of the worksheet where the data table is located to 'Data Dump'):

    ActiveChart.SeriesCollection(1).XValues = Sheets("'Data Dump'").Range("B1002:B2001").Offset(Sheets("'Data Dump'").Range("A1"), 0).Value
    ActiveChart.SeriesCollection(1).Values = Sheets("'Data Dump'").Range("K1002:K2001").Offset(Sheets("'Data Dump'").Range("A1"), 0).Value

    ...and I got the error: "Subscript out of range".

    In looking for the problem, I tried entering the actual value from A1 in the OFFSET block, rather than use a cell reference:

    ActiveChart.SeriesCollection(1).XValues = Sheets("'Data Dump'").Range("B1002:B2001").Offset(1001, 0).Value
    ActiveChart.SeriesCollection(1).Values = Sheets("'Data Dump'").Range("K1002:K2001").Offset(1001, 0).Value

    ...and I got the error: "Subscript out of range".
    Your thoughts?

  4. #4
    Registered User
    Join Date
    11-27-2012
    Location
    Houston
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Macro to change new chart's source data to reference cells.

    NickyC,

    I got it to work. Here's what I ended up with:
    Please Login or Register  to view this content.
    Thank you very much for your help!

+ 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