Solved

Insert a hyphen into category names of data labels using VBA

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

736 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