Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Charting / November 2003

Tip: Looking for answers? Try searching our database.

Jon Peltier's LastPointLabel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Phil Hageman - 05 Nov 2003 14:22 GMT
In Module3 of a workbook (solely dedicated to
LastPointLabel) I have downloaded/copied the following
code from your geocities website:
 Option Explicit
 Sub Create_Menu().
 Sub Delete_Menu().
 Sub LastPointLabel().

Opening the worksheet containing imbedded charts, I run
the LastPointLabel macro and receive: Run-time error '91':
Object variable or With block variable not set.

The line: For Each. in the code is highlighted yellow

Sub LastPointLabel()
Dim mySrs As Series
Dim nPts As Long
For Each mySrs In ActiveChart.SeriesCollection
   With mySrs
       nPts = .Points.Count
       mySrs.Points(nPts).ApplyDataLabels _
           Type:=xlDataLabelsShowValue, _
           AutoText:=True, LegendKey:=False
       mySrs.Points(nPts).DataLabel.Text = mySrs.Name
   End With
Next
End Sub

What do I need to do to make this work?
Jon Peltier - 05 Nov 2003 17:39 GMT
Phil -

You need to select a chart.  A "more advanced" version of this procedure
is listed below.  It checks for an active chart, and it finds the last
point that will accept a label (i.e., the value isn't a blank or #N/A).

Sub LastPointLabel()
Dim mySrs As Series
Dim nPts As Long, iPt As Long
Dim ErrNum As Long
If ActiveChart Is Nothing Then
    MsgBox "Select a chart and try again.", vbExclamation, "No Chart
Selected"
Else
    For Each mySrs In ActiveChart.SeriesCollection
        With mySrs
            nPts = .Points.count
            For iPt = nPts To 1 Step -1
                On Error Resume Next
                mySrs.Points(iPt).HasDataLabel = True
                mySrs.Points(iPt).DataLabel.Text = mySrs.name
                ErrNum = Err.Number
                On Error GoTo 0
                If ErrNum = 0 Then Exit For
            Next
        End With
    Next
End If
End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______

> In Module3 of a workbook (solely dedicated to
> LastPointLabel) I have downloaded/copied the following
[quoted text clipped - 25 lines]
>
> What do I need to do to make this work?
Phil Hageman - 05 Nov 2003 18:21 GMT
IT WORKS GREAT!!  Thank you very much.  Some questions:
 1. Can we make the code work for all charts on a
worksheet - automatically?
 2. Is there a way to specify specific serieses?  My
charts are combination area charts, with lines ploted.  I
want to label two line series that are putting a straight
line across the chart (constant values). These I want to
label, no others.
 3. Is there a way to make the labels permanent where the
user doesn't have to invoke the macro?

 4. Also, when I run the macro on a selected chart, the
chart flickers for a few seconds and then settles.  I'm
sure users will object to this.  Is there a way to avoid
seeing this?

Thanks again Jon for a fantastic feature. Appreciate it.

Phil
>-----Original Message-----
>Phil -
[quoted text clipped - 64 lines]
>
>.
Jon Peltier - 06 Nov 2003 01:20 GMT
Wow, Phil.  You're tough.  Anyway, all of your requests are possible.
Number 3 is toughest, because you'd need to set up worksheet events to
change the labels (and the point being labeled, the number of series
being labeled, etc.) when required.  You may want to use a cell link
instead of a static label (like "=Sheet1!R1C1" instead of mySrs.Name in
this code).

Numbers 1, 2, and 4 are easily integrated:

Sub LastPointLabel()
Dim mySrs As Series
Dim nPts As Long, iPt As Long
Dim ErrNum As Long
Dim ChtOb As ChartObject

'' 4. Stop Screen Flicker (Not sure about effectiveness)
Application.ScreenUpdating = False

'' 1. All Charts on a Worksheet
For Each ChtOb In ActiveSheet.ChartObjects
    For Each mySrs In ChtOb.Chart.SeriesCollection

      '' 2. Line Series Only
      If mySrs.ChartType = xlLine Or _
            mySrs.ChartType = xlLineMarkers Then
        With mySrs
            nPts = .Points.Count
            For iPt = nPts To 1 Step -1
                On Error Resume Next
                mySrs.Points(iPt).HasDataLabel = True
                mySrs.Points(iPt).DataLabel.Text = mySrs.Name
                ErrNum = Err.Number
                On Error GoTo 0
                If ErrNum = 0 Then Exit For
            Next
        End With
      End If
    Next
Next
Application.ScreenUpdating = True
End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______

> IT WORKS GREAT!!  Thank you very much.  Some questions:
>   1. Can we make the code work for all charts on a
[quoted text clipped - 101 lines]
>>
>>.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.