+ Reply to Thread
Results 1 to 15 of 15

Globally Change All Chart Titles' Substring with Macro?

  1. #1
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Globally Change All Chart Titles' Substring with Macro?

    I have to create a large report each month with continuously updated data. In the report are over 16 Excel 2003 charts all with all chart titles containing a string "As of 31 Xxx 2011" where "Xxx" is the standard 3 letter month abbreviation.

    I have been able to find code that will update each chart's data individually, where you click on the chart, run the macro and the series updates etc. But for titles it doesn't work and I could just edit the individual title for the same effect as easily.

    Is it possible for write a macro that will select ALL the charts on the active sheet, find the substring in each title (such as "Mar") and replace it with a second substring (such as "Apr")?

    The data could be hard coded each month in say cells A1 & B1 and the macro updates all the charts from these cells.

    Thanks for any help you may provide.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Globally Change All Chart Titles' Substring with Macro?

    Are the charts in one big workbook or are they embedded in something like a Word Document?
    Martin

  3. #3
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Re: Globally Change All Chart Titles' Substring with Macro?

    They are basically all on one worksheet page, with the data on a separate page.

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Globally Change All Chart Titles' Substring with Macro?

    OK, try this macro

    Please Login or Register  to view this content.
    Open up the VBA editor by hitting ALT F11

    Insert a new module by hitting Insert - Module

    Paste the macro into the empty sheet

    Hit ALT F11 to get back to the worksheet.

    Select the sheet containing the graphs.

    Run the macro by going to tools-macro in Excel 2003 or the view ribbon in Excel 2007.

  5. #5
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Re: Globally Change All Chart Titles' Substring with Macro?

    Thanks for the help.
    However,

    It comes back as an error: (on line that begins with Newtitle = )

    Run-time error '438'
    Object doesn't support this property or method

    I'm running Excel 2003 on Windows XP SP2.

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Globally Change All Chart Titles' Substring with Macro?

    Does this more simple version work?

    Please Login or Register  to view this content.

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

    Re: Globally Change All Chart Titles' Substring with Macro?

    The property is Text not caption.

    This code will link the chart title for all charts on the activesheet to cell C1.
    You can use A1 and B1 to hold any text and the date to be displayed.
    Once the link has been made the charts will automatically update when C1 changes.

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

  8. #8
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Re: Globally Change All Chart Titles' Substring with Macro?

    Thank you - this is very helpful. A variation without VBA is to use the TEXT() function in a cell, where the the formula bar of the chart title references that cell.

    For education's sake, is there a way to select a specific substring (such as "Mar") in a variable length title of every chart on a sheet, each chart title having a different title length and content, and replace that substring with another one (such as "Apr")? I have code that can do it for other objects, say formulas in series' definitions; I was wondering why similar code will not work for chart titles.

  9. #9
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Re: Globally Change All Chart Titles' Substring with Macro?

    Thank you this works now, but it changes all the titles in every chart to the same string - useful, but not my needs unfortunately.

    I thank you for all your effort, and certainly I may not be communicating exactly the problem, but basically I was looking for VBA code to replace a specific substring in the variable length titles of every chart on a sheet, replacing the substring (such as "Mar") with a new one (such as "Apr") but keeping all the other text in each title.

    Thanks again.

  10. #10
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Re: Globally Change All Chart Titles' Substring with Macro?

    Sub ChangeChartTitles()
    Dim MyChart As Chart
    Dim N As Integer
    Dim NewTitle As Variant

    NewTitle = Application.InputBox("Please enter new title", "New chart titles", "As of XXX " & Year(Now()) , , , , , 2)
    If NewTitle = False Then Exit Sub
    For N = 1 To ActiveSheet.ChartObjects.Count
    Set MyChart = ActiveSheet.ChartObjects(N).Chart
    MyChart.ChartTitle.Caption = NewTitle
    Next N
    End Sub


    Thank you this works now, but it changes all the titles in every chart to the same string - useful, but not my needs unfortunately.

    I thank you for all your effort, and certainly I may not be communicating exactly the problem, but basically I was looking for VBA code to replace a specific substring in the variable length titles of every chart on a sheet, replacing the substring (such as "Mar") with a new one (such as "Apr") but keeping all the other text in each title.

    Thanks again.[/QUOTE]

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

    Re: Globally Change All Chart Titles' Substring with Macro?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Please Login or Register  to view this content.
    Assume all of the text at the end of the title text can be replaced.
    You may need to modify the date generated to suit.

  12. #12
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Globally Change All Chart Titles' Substring with Macro?

    Maybe this?

    Please Login or Register  to view this content.
    Old and new values are specified in A1 and A2 respectively.

    Incidently, .Caption appear to work as well (in Excel 2007)

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

    Re: Globally Change All Chart Titles' Substring with Macro?

    Interesting, I had not noticed the addition of the Caption property.

  14. #14
    Registered User
    Join Date
    02-17-2008
    Posts
    21

    Re: Globally Change All Chart Titles' Substring with Macro?

    Quote Originally Posted by mrice View Post
    Maybe this?

    Please Login or Register  to view this content.
    Old and new values are specified in A1 and A2 respectively.

    Incidently, .Caption appear to work as well (in Excel 2007)

    Excellent! This works like a charm.
    Changed the hard coded ranges to Inputbox() variables and I'm all set.
    Many thanks again.

  15. #15
    Registered User
    Join Date
    03-19-2013
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Globally Change All Chart Titles' Substring with Macro?

    Hello, i should like to know if a can replace a substring of a title, but a word given by the user.
    I make epidemiological reports, but i just need to change the name of the pathology, the rest of the title leave it as is.
    ("emergencies january 2013
    neumonia women")
    just want to change neumonia for hypertension for example. There are graphics for men and women in the same sheet.
    I want to ask the user (me) wich word do i want to change and wich word i am going to change with.
    The examples of code given here change te begining or the end of a title.
    Can anybody help me
    Last edited by pagano8; 03-20-2013 at 11:33 AM. Reason: to be more explicit

+ 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