Solved

Insert a hyphen into category names of data labels using VBA

Posted on 2014-03-08
2
484 Views
Last Modified: 2014-03-08
Dear Experts:

below macro ...
... creates a pie chart with data labels showing the category names and the percentage values (see lines 52-54). Everyhing is fine with the macro.

Could somebody please help me tweak this code with the following requirements:

1.
The macro is to loop thru all the data label's category names and perform the following action if the category name equals 'Funktionseinschraenkung'.
In this case the macro has to insert a hyphen after the 9th character so that the data label's category name looks as follows: Funktions-einschraenkung
2.
The macro is to loop thru all the data label's category names and perform the following action if the category name equals 'allg. Oberflaechenschaeden'.
In this case the macro has to insert a hyphen after the 18th character so that the data label's category name looks as follows: allg. Oberflaechen-schaeden.

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

Regards, Andreas

Sub Create_MyChart_01()

Dim MyChart As Chart
Dim Mycht As ChartObject
Dim DataRange As Range

Set DataRange = ActiveSheet.Range("A13:A17", "C13:C17")
Set MyChart = ActiveSheet.Shapes.AddChart.Chart

With MyChart
    .SetSourceData Source:=DataRange
    .ChartType = xlPie
    .HasLegend = False
    .HasTitle = True
    .ChartStyle = 5
End With

With MyChart.Parent
    .Name = "MyChart_01"
    .Width = Application.CentimetersToPoints(12)
    .Height = Application.CentimetersToPoints(12)
    .Left = 12 
    .Top = 80
End With

On Error Resume Next
Set Mycht = ActiveSheet.ChartObjects("MyChart_01")
        With Mycht.Chart
            .PlotArea.Width = 210
            .PlotArea.Height = 210
            .PlotArea.Left = (.ChartArea.Width - .PlotArea.Width) / 2
            .PlotArea.Top = Application.CentimetersToPoints(2.4)
            '.PlotArea.Position = xlChartElementPositionAutomatic
End With
On Error GoTo 0

With ActiveSheet.ChartObjects("MyChart_01").Chart
    .HasTitle = True
End With
With ActiveSheet.ChartObjects("MyChart_01").Chart.ChartTitle.Format.TextFrame2.TextRange
    .Font.Name = "Verdana"
    .Font.Size = 11
    .Font.Bold = True
End With

With ActiveSheet.ChartObjects("MyChart_01").Chart.ChartTitle
    .IncludeInLayout = True
    .FormulaLocal = "=2_Auswertung!$D$7"
End With


MyChart.SeriesCollection(1).ApplyDataLabels AutoText:=True, LegendKey:= _
False, HasLeaderLines:=True, ShowSeriesName:=False, ShowCategoryName:= _
True, ShowValue:=False, ShowPercentage:=True, ShowBubbleSize:=False

With MyChart.SeriesCollection(1).DataLabels
    .Position = xlLabelPositionBestFit 
    .Font.Name = "Verdana"
    .Font.Size = 8.5
End With

End Sub

Open in new window

0
Comment
Question by:AndreasHermle
2 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39914905
Andreas,
I shortened your code by eliminating all the references to the chart name. I also added the requested feature for hyphenation.

I did not test the code however. If it isn't working correctly, would you please post a sample workbook so I can test it?
Sub Create_MyChart_01()

Dim MyChart As Chart
Dim Mycht As ChartObject
Dim DataRange As Range
Dim lbl As DataLabel

Set DataRange = ActiveSheet.Range("A13:A17", "C13:C17")
Set MyChart = ActiveSheet.Shapes.AddChart.Chart
Set Mycht = MyChart.Parent

With MyChart
    .SetSourceData Source:=DataRange
    .ChartType = xlPie
    .HasLegend = False
    .HasTitle = True
    .ChartStyle = 5
End With

With Mycht
    .Name = "MyChart_01"
    .Width = Application.CentimetersToPoints(12)
    .Height = Application.CentimetersToPoints(12)
    .Left = 12
    .Top = 80
End With

With MyChart
    On Error Resume Next
    .PlotArea.Width = 210
    .PlotArea.Height = 210
    .PlotArea.Left = (.ChartArea.Width - .PlotArea.Width) / 2
    .PlotArea.Top = Application.CentimetersToPoints(2.4)
    '.PlotArea.Position = xlChartElementPositionAutomatic
    On Error GoTo 0
    
    .HasTitle = True
    With .ChartTitle.Format.TextFrame2.TextRange
        .Font.Name = "Verdana"
        .Font.Size = 11
        .Font.Bold = True
    End With

    With .ChartTitle
        .IncludeInLayout = True
        .FormulaLocal = "=2_Auswertung!$D$7"
    End With

    With .SeriesCollection(1)
        .ApplyDataLabels AutoText:=True, LegendKey:=False, HasLeaderLines:=True, ShowSeriesName:=False, _
                ShowCategoryName:=True, ShowValue:=False, ShowPercentage:=True, ShowBubbleSize:=False
    
        With .DataLabels
            .Position = xlLabelPositionBestFit
            .Font.Name = "Verdana"
            .Font.Size = 8.5
        End With
        
        For Each lbl In .DataLabels
            lbl.Text = Replace(lbl.Text, "Funktionseinschraenkung", "Funktions-einschraenkung")
            lbl.Text = Replace(lbl.Text, "allg. Oberflaechenschaeden", "allg. Oberflaechen-schaeden")
        Next
    End With
End With

End Sub

Open in new window

Brad
0
 

Author Closing Comment

by:AndreasHermle
ID: 39915087
Hi Brad,

this is very nice of you, cleaning up my code. Looks much better. This code, too, works just great. I really appreciate your professional coding expertise. Thank you very much for it.

Regards, Andreas
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now