Solved

Insert a hyphen into category names of data labels using VBA

Posted on 2014-03-08
2
498 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 81

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

803 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