Results 1 to 2 of 2
  1. #1
    4 Star Lounger
    Join Date
    Jan 2001
    Thanked 1 Time in 1 Post

    VBA code for charts and data labels (Office 2003)

    Is there an easy way to link the data labels of a graph to a different range than the source's range. If the data that makes a bar graph is in A1:C10 but i want the graph to use the range D110 for the data labels, is there an easy way to do this? I know i can use a utility or add-in but i need to add code to an existing project that will do it. Has anyone done this before in a macro? thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 28 Times in 28 Posts

    Re: VBA code for charts and data labels (Office 2003)

    A source range A1:C10 implies that your chart has two series (assuming that column A contains the category names). To which series should the labels in D110 be applied? Here is an example of "fixed" code to set data labels for the first series of the first chart on the active worksheet:

    Sub AddLabels()
    Dim cht As Chart
    Dim ser As Series
    Dim i As Long
    Set cht = ActiveSheet.ChartObjects(1).Chart
    Set ser = cht.SeriesCollection(1)
    ser.HasDataLabels = True
    For i = 1 To ser.Points.Count
    ser.DataLabels(i).Text = Range("D" & i)
    Next i
    Set ser = Nothing
    Set cht = Nothing
    End Sub

    If you need to use this more than once, I would generalize it by adding arguments to the procedure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts