+ Reply to Thread
Results 1 to 9 of 9

VBA to Select Chart Data

  1. #1
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    VBA to Select Chart Data

    Hello,

    I have a ton of charts that I need to update for the new year (the charts are not mine or my design, I'm just trying to help make someones life easier).

    Each chart has to be updated to a specific month. I have a new chart for each month. For example, Chart1 displays data for January, Chart2 displays data for February, and so on...

    Below is a picture of what the data looks like and what I need the code to select (see picture).

    Also to clarify for simplicity, basically any range selected for the "Horizontal (Category) Axis Labels" the rest of the "Legend Entries (Series)" will be the same range one column over.
    For example if "Horizontal (Category) Axis Labels" is Q431:Q461, then "3rd Shift" is R431:R461, "1st Shift" is S431:S461, "2nd Shift" is T431:T461, and "Target" is U431:U461.

    I know I'm asking a lot but I don't have the slightest clue on how to put this code together. I greatly appreciate anyone will to take a look at this for me!
    Data Selector.jpg
    Attached Images Attached Images

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA to Select Chart Data

    Hi there,

    Sorry, but a screenshot like the one you posted really tells us very little about what's involved.

    Can you post your workbook (or even an extract from it) so that we can get a better idea and maybe experiment with it a little?

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    Re: VBA to Select Chart Data

    Ok, here is a sample. January is exactly how I need the rest of the tabs to be for their months.

    Thank you,
    Attached Files Attached Files
    Last edited by Justair07; 01-07-2014 at 09:26 AM.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA to Select Chart Data

    Hi again,

    Take a look at the attached workbook and see if it does what you need.

    The workbook contains only a single graph with a series of Year and Month selection buttons. Clicking on these buttons will cause the graph to display the data for the selected Year/Month combination.

    In general it is much simpler to "drive" a graph from a single range of cells, and to automatically or programatically populate that range with the required data, rather than to assign different ranges to different graphs. It also has the significant advantage that if style or formatting changes are necessary in the future, those changes can be made on a single graph rather than on twelve separate monthly graphs.

    One other minor "benefit" has been included - when the 22-month period covered by the "Data" worksheet needs to be changed (e.g. from 2013-14 to 2015-16) only the starting date (e.g.1/3/2013) in cell B5 of the "Data" workshop should be changed (e.g. to 1/3/2015) - all of the other date cells in that column will be updated automatically. There's even a greyed-out row at the bottom of the table which will become enabled when the period includes a leap year.

    I hope you find the attached workbook useful - please let me know how you get on with it.

    Regards,

    Greg M
    Attached Files Attached Files
    Last edited by Greg M; 01-07-2014 at 01:24 PM. Reason: Minor change to attached workbook

  5. #5
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    Re: VBA to Select Chart Data

    Greg,

    Thank you very much. Your approach is much much more manageable. It's funny how when you work on something for so long you get tunnel vision and over look better and more efficient ways to accomplish the same goal. I do have a question though, is there more code to be seen than just what is in the module (M01_ChartData)? I'd like to understand and learn to duplicate and/or modify to meet certain criteria for future projects.

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA to Select Chart Data

    Hi again,

    Just saw your post (and that you're online ), but right now I'm heading out for the evening.

    I'll certainly reply to you, but it'll be later tonight or tomorrow morning.

    Thanks for your feedback.

    Regards,

    Greg M

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA to Select Chart Data

    Hi again,

    Yes, the only VBA code is that contained in the "M01_ChartData" module.

    This is one of those occasions where "native" Excel does about 97% of the work, and the VBA is there just to provide a prettier user-interface.

    Aside from the VBA code, the application boils down to assigning the table on the "Graph Data" worksheet to the graph, and then using appropriate VLOOKUP formulas in the table to retrieve data for the month/year selected by the User. You can manually insert values into the "Selected Year" and "Selected Month" cells and observe how the values in the table are updated appropriately.

    The VBA code used is simple, but elegant, in that all of the month buttons call the same "InsertMonth" macro, and both of the year buttons call the same "InsertYear" macro. The key to how it works is the use of the Application.Caller property which returns the name of the button which called the routine - the names of all of the month buttons contain the three-letter version of the associated month name, and this value is inserted into the "Selected Month" cell. A similar but very slightly different process is used for the year buttons.

    Incidentally, here's a slightly nicer version of the "InsertYear" routine:

    Please Login or Register  to view this content.
    You can also use the above technique in conjunction with the month buttons if you prefer. The fundamental difference between the month buttons and the year buttons is that the text (i.e. month name) on the month buttons never changes, whereas the text (i.e. year) on the year buttons is determined by the ranges of the date cells listed on the "Data" worksheet.

    Hope you find the above helpful - please let me know how you get on with it.

    Regards,

    Greg M
    Last edited by Greg M; 01-07-2014 at 09:39 PM. Reason: Note re "fundamental difference" added

  8. #8
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    Re: VBA to Select Chart Data

    Great! Extremely helpful! Thank you very much for all the help.

  9. #9
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA to Select Chart Data

    Hi again,

    You're very welcome.

    Many thanks for your feedback - I'm glad that I was able to help out.

    Best regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Use macro to Select data for a chart
    By gordonf35 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2012, 07:35 PM
  2. Replies: 0
    Last Post: 07-12-2012, 11:27 AM
  3. How to select data for a chart, from another workbook?
    By AluminumHaste in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 11-27-2008, 02:04 AM
  4. [SOLVED] drawing chart only of select data
    By [email protected] in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 04-18-2006, 04:10 AM
  5. [SOLVED] Select all chart data points
    By CL in forum Excel General
    Replies: 2
    Last Post: 01-26-2005, 10:06 AM

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