+ Reply to Thread
Results 1 to 2 of 2

Attach Labels to Points on Scatter Chart

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    20

    Attach Labels to Points on Scatter Chart

    Hi, I am using the following code to assign labels on a scatter chart but am having trouble with one of the lines. I sometimes receive an error on the following line and other times it works fine:
    "xVals = ActiveChart.SeriesCollection(1).Formula"
    The error message is "Object variable or With Block variable not set"
    I was wondering if someone might be able to help by explaining the cause of the problem?

    Many thanks.


    Sub AttachLabelsToPoints()
    
       'Dimension variables.
       Dim Counter As Integer, ChartName As String, xVals As String
    
       ' Disable screen updating while the subroutine is run.
       Application.ScreenUpdating = False
    
       'Store the formula for the first series in "xVals".
       xVals = ActiveChart.SeriesCollection(1).Formula
    
       'Extract the range for the data from xVals.
       xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, _
          Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
       xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
       Do While Left(xVals, 1) = ","
          xVals = Mid(xVals, 2)
       Loop
    
       'Attach a label to each data point in the chart.
       For Counter = 1 To Range(xVals).Cells.Count
         ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = _
             True
          ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text = _
             Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
       Next Counter
    
    End Sub
    Last edited by ryanpetersen; 12-09-2014 at 06:07 PM. Reason: Updated with code tags

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

    Re: Attach Labels to Points on Scatter Chart

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)


    The error would suggest that no chart is currently selected so the Activechart object is nothing
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel 2007 : Labels for Data Points on a Scatter Chart
    By ProfessorDJF in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 09-12-2018, 12:47 PM
  2. Attach Labels to points in Scatter chart
    By Roop in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2016, 04:45 AM
  3. Adding Labels to Data Points in Scatter Plot Chart
    By Stopea in forum Excel General
    Replies: 10
    Last Post: 05-21-2014, 08:25 PM
  4. Replies: 0
    Last Post: 04-01-2009, 10:54 AM
  5. Macro to add labels to data points in an xy scatter chart
    By Scott Wagner in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 12-16-2005, 09:50 PM

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