LegendEntries Index in PieChart & Delete Condition

tomay62

New Member
Joined
Jun 17, 2016
Messages
2
I've been a few days trying to find the mistake or what I'm doing Wrong. I show here the current output and what it should be. I think maybe i'm defining the range with some mistake, or maybe <code>LegendEntries(i)</code>, doeasn't work with the same index as <code>Points(i)</code>. Any suggestion ? Or maybe how could I do this from another approach ?
The thing here is to get a chart and delete the legend and labels associated with "0" values.
The First graphic is my current output. The second one is my desire output, and what I expected to get with my code.
The Legends that have 0 values are "Asia" and "Latam", but my current output is throwing "Latam" and it should throw "RoW", because "RoW" insn't associated with 0 values, its value is "5.0%".



<tbody>
</tbody>

Code:
<code>Sub CreateChart()
On Error Resume Next
Sheets(1).ChartObjects.Delete

    Dim MyChart As Chart
    Dim rowi As Integer
    Dim MyRange As Range


rowi = Range("I7").Row ' Index to select last row non empty, in this example it's row number 11.
Do While Sheets(1).Cells(rowi, Range("I7").Column).Value = _
IsEmpty(Cells(rowi, Range("I7").Column))
rowi = rowi + 1
Loop

'----------------------------------------------------CHART---------------------------------------------------------------

Set MyRange = Range("I6:M6" & ",I" & rowi & ":M" & rowi)

Set MyChart = Sheets(1).Shapes.AddChart(xlPie).Chart

     MyChart.SetSourceData Source:=MyRange

With MyChart.SeriesCollection(1)
 .HasDataLabels = True
 .DataLabels.NumberFormat = "0.0%"
End With

With MyChart
 .HasLegend = True
End With

For i = 1 To (Range("M6").Column - Range("I6").Column + 1)

If  Cells(rowi, Range("I6").Column + i - 1).Value = 0 Then
MyChart.SeriesCollection(1).Points(i).DataLabel.Delete
MyChart.Legend.LegendEntries(i).Delete
End If

Next i
End Sub</code>



-- removed inline image ---





-- removed inline image ---
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The Images for the example applied to the code

Img1.jpg
[/URL][/IMG]

img2.jpg
[/URL][/IMG]
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,923
Members
449,479
Latest member
nana abanyin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top