<tbody>
[TD="class: votecell"][/TD]
[TD="class: postcell"] 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%".
[/TD]
</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 ---