Solved

VBA macro (Excel) to create pie chart with legends that exclude 0% values

Posted on 2014-07-21
4
398 Views
Last Modified: 2014-07-28
Hi!
In a worksheet I've got a simple 2-columns table (name + value); the values  can occasionally drop to 0.0%. I'm looking for a macro (VBA) which can reproduce a pie chart on the same sheet which does not show the 0.0% figures - neither the value, nor the label (name).
The below macro does indeed erase the 0%-labels. However, instead of displaying for each label (name) the appendant value as listed in the table, the macro produces percentages referring to the total of this pie charts' values.  

I tinkered with the command
        Type:=xlShowLabelAndPercent
(e.g. starting with "Type:= xlDataLabelsShowValue" and take it from there, but got nowhere...).

Any help would be highly appreciated, thank you.
Karl




Sub ClearLabels()

       Worksheets("abc").ChartObjects(1).Chart.ApplyDataLabels _
        Type:=xlShowLabelAndPercent
   
      For Each x In Worksheets("abc").ChartObjects(1). _
         Chart.SeriesCollection(1).Points

            If InStr(x.DataLabel.Text, Chr(10) & "0.0") > 0 Then
            x.DataLabel.Delete
         End If
      Next
   End Sub
0
Comment
Question by:CandidKarlito
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40211795
Hi,

pls try

    Worksheets("abc").ChartObjects(1).Chart.ApplyDataLabels xlDataLabelsShowValue, , , , , True

Regards
0
 

Author Comment

by:CandidKarlito
ID: 40220159
Hello 71!
Tks for your line - and sorry for my delayed reply.

Please have a look at the attached Xls. I have added a mini pie chart to illustrate what I need ("intended result").
I added your command into the macro but this didn't work. Most likely my own fault.

Your advice will be highly appreciated.
Best Karl
TEST---macro-pie-chart-legends.xls
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40221074
pls try

 Sub ClearLabels()

        Worksheets("abc").ChartObjects(1).Chart.ApplyDataLabels xlDataLabelsShowValue, , , , , True
   
       For Each x In Worksheets("abc").ChartObjects(1). _
          Chart.SeriesCollection(1).Points

             If InStr(x.DataLabel.Text, " 0.0") > 0 Then
             x.DataLabel.Delete
          End If
       Next
    End Sub

Regards
0
 

Author Closing Comment

by:CandidKarlito
ID: 40223573
Thank you very much, Rgonzo1971, for your fast  response - and your solution!
This macro of yours is short and handy, works  fine in all my large excel pie charts!

Best regards
Karl
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

816 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

10 Experts available now in Live!

Get 1:1 Help Now