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.

Label Last Point – Updated Add-In

by Jon Peltier
Thursday, November 12th, 2009
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

On my web site I wrote a tutorial that showed how to Label the Last Point in an Excel Chart’s Series, and I posted a small add-in with the labeling procedure. I later blogged about this technique in Label Each Series in a Chart. When Excel 2007 came around, I discovered that the code no longer worked as expected because Excel 2007 treats unplottable points (blank cells or #N/A errors) differently than Excel 2003 had. I figured out a workaround and blogged about it in Label Last Point for Excel 2007. I never posted the corresponding add-in for Excel 2007.

The problem with the code in these pages is that they only labeled the active chart. The problem with this is that I often have a dozen or more charts to label, and it’s tedious to select and run the code, select and run the code, select and run the code, etc. So I recently made a minor adjustment to the code that would apply last point labels to the active chart, or to all selected charts. (You can select multiple charts by holding Shift or Ctrl while clicking on them.)

It’s almost embarrassing how long it took me to enhance the code. The reason I didn’t is another “feature” of Excel 2007. In previous versions of Excel, when I wanted to carry out an action on the selected charts on a worksheet, I could check out the selected shapes, determine which shapes were chart objects, and act on each chart object.

      Dim myChart As ChartObject
      Dim myShape As Shape

      ''' SELECTED SHAPES ON THE SHEET
      For Each myShape In Selection.ShapeRange
        ''' IS SHAPE A CHART?
        On Error Resume Next
        Set myChart = ActiveSheet.ChartObjects(myShape.Name)
        If Not Err Then
          ' check for surface chart (disallowed)
          If myChart.Chart.ChartType < 83 Or myChart.Chart.ChartType > 86 Then
            ''''''''''
            '' do what needs to be done
            ''''''''''
          End If
        End If
        On Error GoTo 0
      Next

For some reason, Excel 2007 was unable to detect which chart objects were selected, so it would process every chart object on the sheet. Not cool if you have painstakingly added a dummy series and applied custom data labels to the chart you had not selected.

I learned that you could add a shape to the sheet, and add this shape to the selection, and Excel 2007 would recognize which chart objects were actually selected. But this is a bit more contrived than I like. Not as elegant as my usual code (ha ha).

Leave it to my friend and colleague Andy Pope to find a workaround. Andy is great at looking at things a bit sideways and seeing what the rest of us have overlooked. Visit Andy’s site for a bunch of imaginative charting examples.

Anyway, Andy realized that the selection would consist of a set of objects, and you can test whether each object in the selection is a chart object. This approach works just fine in 2007 (and in 2003!).

      Dim obj As Object

      '' check for error (i.e., selection isn't a bunch of shapes)
      On Error Resume Next
      For Each obj In Selection
        If TypeName(obj) = "ChartObject" Then
          ' check for surface chart (disallowed)
          If obj.Chart.ChartType < 83 Or obj.Chart.ChartType > 86 Then
            ''''''''''
            '' do what needs to be done
            ''''''''''
          End If
        End If
      Next
      On Error GoTo 0

So finally I expanded my routine. I’ve packed it into an add-in, which works just fine in Excel 2003 and 2007. Download the zip file PTSLabelLastPoint.zip and unpack the add-in (PTSLabelLastPoint.xla). Install it using the applicable protocol in Installing an Excel Add-In in Excel 2003 or in Installing an Add-In in Excel 2007.

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 DaleW
Time: Friday, November 13, 2009, 1:29 pm

Jon,

I noticed your add-in here doesn’t have the exact same functionality as your original tutorial code for Excel 2003.

With your new PTS Charts add-in, existing series labels are no longer reset to the SeriesName, which can be handy when one wants to substitute Label Last Point for a legend. This different behavior only arises if one has labeled a series or chart or last point with something other than the series name along the way. For instance, if someone wants to fix their space-consuming legends and cluttered data value labels, they have to turn off the point labels manually before the new Label Last Point can work its magic.


Comment from Jon Peltier
Time: Friday, November 13, 2009, 9:17 pm

Hi Dale -

Seems to be a version control issue one my end. The problem is that I have a number of versions of this utility, including one in my personal macro add-in and more than one standalone. I need to get the latest and greatest before I add this last functionality.


Comment from Jeremy
Time: Saturday, November 14, 2009, 8:07 pm

Hi, Jon,

Thanks for posting this new code. Viewing the code is always a great learning experience for me. If you have some time, I’d really love to see a post about how to make add-ins. I often share macros with other folks in my organization by pasting code in an email. It would be really nice to compile some of the better macros and distribute to the less technical colleagues as an add-in. Thanks!


Comment from Jon Peltier
Time: Sunday, November 15, 2009, 9:39 pm

Dale -

The problem was easy to fix. When the point already has a label with a formula (maybe any label, I didn’t check), the AddDataLabel method doesn’t change the label.

The solution? Delete the label before adding the label that is called for in the program.

I’ve updated a corrected version of the utility.


Comment from Ignacio
Time: Wednesday, May 5, 2010, 10:47 am

Hello Jon,

Thanks a lot for posting your work. It has been very useful to me!

I am writing because I have two questions:
1. Could you please post the macro for Excel 2007 that includes the adjustment to the code that would apply last point labels to the active chart or to all selected charts?
2. I know how to install an Add-in… but I do not know how to “unpack the add-in (PTSLabelLastPoint.xla)”. Could you please explain the process of downloading and unpacking the add-in?

Best regards,

Ignacio


Comment from Jon Peltier
Time: Wednesday, May 5, 2010, 10:56 am

Ignacio -

I meant unzip the downloaded zip file to extract the add-in file (PTSLabelLastPoint.xla), then use the protocol at the link provided to install the file as an Excel add-in.

The code you’ve requested is in the add-in.

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.