Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Reformat Data Labels using VBA

Dear Experts:

the attached sample file shows bar charts where ...
... the DATA LABELS contain the category name as well as the values separated by a semicolon. Position is 'Outside End'

Is it possible to manipulate the data labels of the charts in the following way, using VBA:
All data labels where ...
.. the category name is 1 and 2 have to be positioned inside ('Inside End') and ...
... the font color has to be changed to white. The other ones have to be left untouched.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

 DataLabels-Reformat.xlsm
0
AndreasHermle
Asked:
AndreasHermle
1 Solution
 
pteranodon72Commented:
AndreasHermle,

It looks like this should do it:

Dim c As Excel.Chart
Dim dl As Excel.DataLabel
Set c = {reference to the chart to update}
Set dl = c.SeriesCollection(1).Points(1).DataLabel
dl.Position = xlLabelPositionInsideEnd
dl.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = vbWhite

Set dl = c.SeriesCollection(1).Points(2).DataLabel
dl.Position = xlLabelPositionInsideEnd
dl.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = vbWhite

Set dl = Nothing
Set c = Nothing

Open in new window

HTH,
pT72
0
 
AndreasHermleAuthor Commented:
Hi pT72:

great job, exactly like I wanted it.

I slightly tweaked the code since to be able to apply it to all the charts of the active worksheet (see below).

Thank you very much for your professional help.

Regards, Andreas
Sub Reformat_Data_Labels()

Dim dl As Excel.DataLabel
Dim myChtObj As ChartObject

For Each myChtObj In ActiveSheet.ChartObjects
        Set dl = myChtObj.Chart.SeriesCollection(1).Points(1).DataLabel
        dl.Position = xlLabelPositionInsideEnd
        dl.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = vbWhite
        Set dl = myChtObj.Chart.SeriesCollection(1).Points(2).DataLabel
        dl.Position = xlLabelPositionInsideEnd
        dl.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = vbWhite
Next

End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now