Consulting

Results 1 to 9 of 9

Thread: Aligning data labels in a chart

  1. #1
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location

    Aligning data labels in a chart

    I pretty much have my chart setup the way I want (I think), but I always have to manually position the percentages. Is there a better way to set this up so I don't have to do that?
    Office 2010, Windows 7
    goal: to learn the most efficient way

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do it in Excel with the macro recorder on, and you will have the code.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    oh wow, that was rather easy. I guess I didn't think of doing it through code. I was thinking there might be a way to do it through the chart setup.

    Now I just have to cycle through all the data labels, get their top and left, assign it to the % labels and adjust the left position to the right a little bit.

    Thanks ...
    Office 2010, Windows 7
    goal: to learn the most efficient way

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have written code like that before, and I beleieve you just grab the series and align the the whole set of labeles, like so (off the top}

    Dim i As Long
    With ActiveSheet.ChartObjects("Chart 1").Chart
    For i = 1 To .SeriesCollection.Count
    With .SeriesCollection(i).DataLabels
                    .VerticalAlignment = xlCenter
                End With
            Next i
        End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    oh but i want the % label to line up with it's corresponding series label. For instance 63% would line up with 25.00 in the chart above. I think I have to get the specific top/left of that label to line it up, wouldn't I? (instead of using xlcenter)
    Office 2010, Windows 7
    goal: to learn the most efficient way

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How were the percentages setup to the chart?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    they were just data labels of separate series on 2ndary vertical axis.

    Here's the code I came up with. Seems to work but if you wanna tweak it, be my guest.

    Sub AlignPercentages()
    With ActiveSheet.ChartObjects("Chart 1").Chart
            For j = 1 To 6 Step 2
                For i = 1 To 3
                    .SeriesCollection(j).Points(i).DataLabel.Select
                    mytop = Selection.Top
                    myleft = Selection.Left + 35
                    .SeriesCollection(j + 1).Points(i).DataLabel.Select
                    Selection.Top = mytop
                    Selection.Left = myleft
                Next i
            Next j
        End With
    End Sub
    Office 2010, Windows 7
    goal: to learn the most efficient way

  8. #8
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    and here's my file in Excel 2010 that I was using as a tester.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If it works, why would I mess with it
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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