?
Solved

Insert a hyphen into category names of data labels using VBA

Posted on 2014-03-08
2
Medium Priority
?
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

762 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