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
Solved

Insert a hyphen into category names of data labels using VBA

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

808 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