+ Reply to Thread
Results 1 to 10 of 10

A chart macro that can run on multiple sheets

  1. #1
    Registered User
    Join Date
    03-02-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    5

    A chart macro that can run on multiple sheets

    Sorry for what is probably a newbie question, but I have searched around for hours on the web and haven't found a solution.

    I have a workbook with a hundred or so sheets. Each sheet has data in cells A1:F5. I'd like to use a macro that will allow me to go to a sheet and create a column chart. The title of the chart is to be read from cell A8.

    I've used Excel to record the following macro:

    Sub create_chart()
    '
    ' create_chart Macro
    '
    ' Keyboard Shortcut: Ctrl+o
    '
    Range("A1:F5").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range("test!$A$1:$F$5")
    ActiveSheet.Shapes("Chart 14").ScaleWidth 1.5266666667, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 14").ScaleHeight 1.5902777778, msoFalse, _
    msoScaleFromTopLeft
    ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    Selection.Caption = "horizontal axis title"
    ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
    Selection.Caption = "vertical axis title"
    ActiveChart.SetElement (msoElementChartTitleAboveChart)
    Selection.Caption = "=test!R8C1"
    Selection.Format.TextFrame2.TextRange.Font.Size = 14
    ActiveChart.ChartArea.Select
    ActiveSheet.Shapes("Chart 14").Line.Visible = msoFalse
    End Sub




    I'm running into a couple problems with this.

    One is that it refers specifically to sheet "test", but I need it to apply to whatever sheet I happen to be looking at. That goes for the caption title as well.

    The other thing is that it refers to "Chart 14" (you can tell that I've been trying this multiple times, creating and deleting charts as I go along). When I try running it on a different sheet, I get an error message that the item with the specified name wasn't found, and debug shows that it's referring to the chart name.

    How do I generalize the macro above so that it's not tied to a specific sheet name or chart number?

    Thanks much for any pointers.

  2. #2
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: A chart macro that can run on multiple sheets

    I guess my question back would be - Why are you trying to make a macro combine data in a chart? The most efficient way of doing this would be to pull the data into one sheet, let the formulas continuously link to your several other sheets to compile in a clean custom format for your chart. Automating a chart you will never get the level of detail/customization that you can get just by making a new sheet, pulling the data into a predefined layout and customizing your chart with the precise colors, layout and formatting. Then all you have to do is hit Refresh (Really if you have auto calc on) you only need to update the underlying data, nothing to run beyond that...
    -If you think you are done, Start over - ELeGault

  3. #3
    Registered User
    Join Date
    03-02-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    5

    Re: A chart macro that can run on multiple sheets

    Thanks for the quick response, but I'm not sure I understand it.

    I'm just trying to create a macro that quickly charts the data in a sheet. I have about 50 sheets with the same data layout so the macro would be useful for creating a separate chart in each sheet.

    Whether the data are in 50 sheets or in one sheet, I still have separate 50 charts to create.

    Not sure what I'm missing here?

  4. #4
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: A chart macro that can run on multiple sheets

    What is the use of this document - Will you be copying the data out of it... or will there be an end user to browse each sheet - I just don't see the need to fill a document with multiple sheets if they are the same information just pertaining to different time periods. It would be a much better setup, both visually and for memory, if you were to combine the data into one log, make a sheet that then calculates the information you wish to view or the end user would like to see and that be the end of it. The Graph would always be right there and you would be able to see it refresh with a selection of time or category depending on your data.

    The answer to your original question is yes, you could make a list of the sheets and create a loop in which would copy the chart object, rename the object the same name as the current sheet then go to the next and repeat until it hits the end of your list. But I want to reiterate that I think you have a setup that is counter intuitive and is hard on excel. If you can post a sample workbook with a few tabs (Not all 50 that is way too much) so that we can get an idea of what it is you have setup and whether what I am saying makes sense or if you truly need to go with 50 sheets....

  5. #5
    Registered User
    Join Date
    03-02-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    5

    Re: A chart macro that can run on multiple sheets

    An example is attached. There are 3 sheets, each with different data
    that correspond to different cases. Each case has a different chart
    associated with it. The charts will be used in a separate presentation
    (one presentation with 50-100 charts in it).

    I just want a methodology to create the charts quickly with the
    consistent sizing and formatting that I want. In the time
    that I have spent researching this and fooling around with the
    VBA script, I'm sure I could have done the whole thing manually.
    But it will probably come up again in the future.

    Thanks.
    Attached Files Attached Files

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

    Re: A chart macro that can run on multiple sheets

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  7. #7
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: A chart macro that can run on multiple sheets

    If no one else has picked this up I will take a look later - My PC is running several nightly reports so I cannot utilize excel to even peek at your file ATM -

    I would say go read some tutorials on looping (If you don't know how to do so already) something like Loops and then apply that to your macro.
    What I would do (Again have not looked at the file) but I would write a quick macro to run through each sheet and have it copy the original chart and then redefine the data as well as rename the chart.

    Re record your macro so that you capture how to redefine an area, just right click on it and select data, change it to the new sheet name.

    That can be done by defining what the sheet name is and then calling on it in a short line
    Please Login or Register  to view this content.
    By you can then call on that chart (Which is where you were having issues before) because it will be a controlled name. Now of course if you have multiple charts on a sheet, just add numbers up to the max... Do this through loops or physically add them (Loops are best but that comes down to where you fell most comfortable.

    Again, if no one else tackles this I will take a peek later - really hard to do without opening your file and diving in.

    Hope that helps a bit at the very least...


    Cheers
    Last edited by ELeGault; 03-03-2015 at 04:46 AM.

  8. #8
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: A chart macro that can run on multiple sheets

    Hah should have refreshed - Good luck!

  9. #9
    Registered User
    Join Date
    03-02-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    5

    Re: A chart macro that can run on multiple sheets

    Andy, thank you very much, it works perfectly!

    I just have one followup question. How do I modify CreateChart() to allow
    me to re-run it on a single sheet. I had a couple sheets where the data
    layout was not quite the same as on other sheets, and this caused problems
    for the chart generation. I'd just like to re-run it on those individual sheets.

    Thank you!

  10. #10
    Registered User
    Join Date
    03-02-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    5

    Re: A chart macro that can run on multiple sheets

    Never mind, I figured it out, I think.

    Thanks again for the help.

+ 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. Replies: 2
    Last Post: 09-19-2013, 04:22 AM
  2. Macro for plotting chart on Multiple sheets
    By mrigtrishna in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-23-2012, 06:02 AM
  3. Creating a Chart in Multiple Sheets using a Macro
    By GeauxGeo in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 12-14-2010, 06:48 AM
  4. One chart for multiple sheets?
    By RB89 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-19-2008, 04:40 AM
  5. Multiple sheets using single chart
    By roumi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2005, 02:03 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