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.
Are the charts in one big workbook or are they embedded in something like a Word Document?
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
They are basically all on one worksheet page, with the data on a separate page.
OK, try this macro
Open up the VBA editor by hitting ALT F11Sub 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 " & Format(WorksheetFunction.EoMonth(Now(), -1), "dd mmm yyyy"), , , , , 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
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.
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
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.
Does this more simple version work?
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
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
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.
Sub ChangeChartTitles() Dim objCht As ChartObject For Each objCht In ActiveSheet.ChartObjects With objCht.Chart .HasTitle = True .ChartTitle.Text = "='" & .Parent.Parent.Name & "'!R1C3" End With Next End Sub
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.
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.
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]
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
Assume all of the text at the end of the title text can be replaced.Sub ChangeChartTitles() Dim objCht As ChartObject dim lngPos as long Const FIND_TEXT = "As of " For Each objCht In ActiveSheet.ChartObjects With objCht.Chart lngPos = Instr(.ChartTitle.Text,FIND_TEXT) .ChartTitle.Text = left(.chartTitle.text,lngpos-1) & _ "As of " & _ format(Dateserial(year(now),month(now),0),"dd mmm yyyy") End With Next End Sub
You may need to modify the date generated to suit.
Maybe this?
Old and new values are specified in A1 and A2 respectively.Sub ChangeChartTitles() Dim objCht As ChartObject Dim lngPos As Long Dim OldValue As String Dim NewValue As String OldValue = Range("A1").Value NewValue = Range("A2").Value For Each objCht In ActiveSheet.ChartObjects With objCht.Chart .ChartTitle.Text = WorksheetFunction.Substitute(.ChartTitle.Text, OldValue, NewValue) End With Next End Sub
Incidently, .Caption appear to work as well (in Excel 2007)
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
Interesting, I had not noticed the addition of the Caption property.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks