Peltier Tech Blog

Excel Chart Add-Ins | Training | Custom Solutions | Charts and Tutorials | PTS Blog

 

Main menu:

 
Peltier Tech
Chart Add-Ins

Peltier Tech Waterfall Chart Utility
Peltier Tech Cluster-Stack Chart Utility
Peltier Tech Box and Whisker Chart Utility
Peltier Tech Marimekko Chart Utility
Peltier Tech Dot Plot Utility
Peltier Tech Cascade Chart Utility

 
Excel Dashboards

Subscribe

Site search

Subscribe

Site search


Recent Posts

Recently Commented

Popular Posts

Archive


 

Categories


 

Privacy Policy

Creative Commons License
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Apply Chart Formatting to Other Charts

by Jon Peltier
Friday, August 21st, 2009
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Sometimes you have a whole workbook full of charts. You made some last week, and others yesterday, but the one you made today has just the right look to it. You’d like to apply the formatting to the rest of the charts, but the thought of reformatting all of those charts makes your brain ache.

The good news is that you don’t need to reformat every little element of every last chart. When you copy a chart, you can select another chart and go to Paste Special. The options in the Paste Special dialog are to paste the formulas (the data in the copied chart), the formats (from the copied chart), or both data and formatting.

paste special dialog for charts

Not only can you paste formats from one chart to another, you can write a little VBA to automate the process.

John Mansfield did just that in VBA to Copy Embedded Chart Formatting in his cellMatrix.net blog. I punched up John’s original code to get this procedure:

Sub Copy_Chart_Formats()

    Dim Sht As Worksheet
    Dim Cht As ChartObject

    Application.ScreenUpdating = False

    ActiveChart.ChartArea.Copy

    For Each Sht In ActiveWorkbook.Worksheets
        For Each Cht In Sht.ChartObjects
            Cht.Chart.Paste Type:=xlFormats
        Next Cht
    Next Sht

    Application.ScreenUpdating = True

End Sub
 

Let’s see how it works. Here are four charts, with different data and with different original chart types and formatting.

original four charts

With the top left chart selected, running the program changes none of the data but all of the formatting of the charts (including the top left chart).

formats copied onto the charts

Closer inspection reveals the bad news, which is that not only are the formats changed to match the master chart, but so are the chart and axis titles. This would be horrible, to get the formats you wanted in all 200 charts in your workbook, but to also have 200 sets of titles to change back.

Fortunately we’re all expert programmers, with a thorough knowledge of Excel’s object model, so we recognize before even trashing all of our charts what steps we need to take.

First we’ll do a little check before reformatting a chart, to save us the fraction of a second it takes to redundantly reformat the original chart. Then we’ll extract the chart and axis titles, apply the master format to the chart (which hoses the titles), and finally reapply the titles.

Sub Copy_Chart_Formats_Not_Titles()

  Dim Sht As Worksheet
  Dim Cht As ChartObject
  Dim chtMaster As Chart
  Dim bTitle As Boolean
  Dim bXTitle As Boolean
  Dim bYTitle As Boolean
  Dim sTitle As String
  Dim sXTitle As String
  Dim sYTitle As String

  Application.ScreenUpdating = False

  Set chtMaster = ActiveChart

  For Each Sht In ActiveWorkbook.Worksheets
    For Each Cht In Sht.ChartObjects
      If Sht.Name = chtMaster.Parent.Parent.Name And _
          Cht.Name = chtMaster.Parent.Name Then
        ' don't waste time on chtMaster
      Else
        With Cht.Chart
          ' get titles
          bTitle = .HasTitle
          If bTitle Then
            ' chart title exists
            sTitle = .ChartTitle.Characters.Text
          End If
          If .HasAxis(xlCategory) Then
            bXTitle = .Axes(xlCategory).HasTitle
            If bXTitle Then
              ' axis title exists
              sXTitle = .Axes(xlCategory).AxisTitle.Characters.Text
            End If
          End If
          If .HasAxis(xlValue) Then
            bYTitle = .Axes(xlValue).HasTitle
            If bYTitle Then
              ' axis title exists
              sYTitle = .Axes(xlValue).AxisTitle.Characters.Text
            End If
          End If

          ' apply formats
          chtMaster.ChartArea.Copy
          .Paste Type:=xlFormats

          ' restore titles
          If bTitle Then
            .HasTitle = True
            .ChartTitle.Characters.Text = sTitle
          End If
          If bXTitle Then
            .Axes(xlCategory).HasTitle = True
            .Axes(xlCategory).AxisTitle.Characters.Text = sXTitle
          End If
          If bYTitle Then
            .Axes(xlValue).HasTitle = True
            .Axes(xlValue).AxisTitle.Characters.Text = sYTitle
          End If
        End With
      End If
    Next Cht
  Next Sht

  Application.ScreenUpdating = True

End Sub

As before, the master chart is selected, then its formatting is applied to all of the other charts in the workbook.

formats copied onto the charts and titles restored

Nice, we’ve changed all of the formats while retaining the axis and chart titles. Instead of three minutes to reformat each chart (longer in 2007 because the F4 shortcut key for Repeat Last Action is no longer an effective timesaver), the whole process took seconds. Maybe a minute to copy and paste this code.

Related Posts:

Bookmark and share this entry:
  • Twitter
  • Digg
  • Facebook
  • LinkedIn
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Google Bookmarks
  • Reddit
  • MySpace
  • Slashdot
  • Yahoo! Buzz
  • Tumblr

Learn how to create Excel dashboards.

Comments


Comment from Jon Peltier
Time: Friday, August 21, 2009, 10:13 am

Ray -

Yep, everyone has their own requirements, I guess.

To do just the activesheet, replace this:

For Each Sht In ActiveWorkbook.Worksheets

with this:

Set Sht = ActiveSheet

and remove this:

Next Sht

Other options need similar changes. I imagine choosing both an active chart as the master chart and choosing only some charts to reformat would be a challenge, requiring some kind of dialog system.


Comment from Ray
Time: Friday, August 21, 2009, 9:38 am

Hi Jon -

Great tip, VERY useful … but what if you don’t want to change *every* chart in the *whole* workbook? Maybe just the selected charts or just the ones the active-sheet? A small pop-up box to solicit user-preferences perhaps?

Thanks alot!

//ray


Comment from JP
Time: Friday, August 21, 2009, 11:00 am

“Fortunately we’re all expert programmers, with a thorough knowledge of Excel’s object model, so we recognize before even trashing all of our charts what steps we need to take.”

:)

I wonder if a response or followup from the “OP” is forthcoming.


Comment from Mike Alexander
Time: Friday, August 21, 2009, 11:58 am

Nice one Jon. You should make this part of the dashboarding bootcamp! VBA day.


Comment from AdamV
Time: Friday, August 21, 2009, 12:07 pm

Nice piece of code – as you say, this could be a jumping-off point for people with their own specific requirements.
For example, you could change the name of one chart to “Master” or somesuch, then find this one to copy formats from, and iterate through all the selected charts to apply the formats to.
This does seem to be one of the places where F4 (or CTRL-Y) seems to work just fine, as the last action is the paste special, so you are not plagued with the issues which beset the non-modal formatting dialog boxes.


Comment from John Mansfield
Time: Friday, August 21, 2009, 2:52 pm

Very nice Jon. I tend to use worksheet cells for titles and totally missed that one.

I didn’t think I wrote the procedure very well to begin with and am glad you were able to improve it.

Thanks again.


Comment from Patricio Cuaron
Time: Tuesday, August 25, 2009, 3:40 pm

Great code, but in xl2007 sp2 it copies the format AND the data of the charts. Can’t figure out why yet.


Comment from Jon Peltier
Time: Thursday, August 27, 2009, 1:05 pm

Patricio -

This happens because Excel 2007 SP2 apparently has a bug. It doesn’t properly recognize xlFormats or xlPasteFormats. This bug is also present in Excel 2007 SP1 and presumably all Excel 2007 versions.

Write a comment

I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.

If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.

Read the PTS Blog Comment Policy.





Subscribe without commenting

Peltier Tech Waterfall Chart Utility Peltier Tech Cluster-Stack Chart Utility Peltier Tech Box and Whisker Chart Utility Peltier Tech Marimekko Chart Utility Peltier Tech Dot Plot Utility Peltier Tech Cascade Chart Utility

Create Excel dashboards quickly with Plug-N-Play reports.