Solved

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

Posted on 2014-07-21
4
392 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 48

Expert Comment

by:Rgonzo1971
Comment Utility
Hi,

pls try

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

Regards
0
 

Author Comment

by:CandidKarlito
Comment Utility
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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

12 Experts available now in Live!

Get 1:1 Help Now