Check if all legend items are displayed

kaRool69

New Member
Joined
Oct 23, 2014
Messages
2
Is there a way to see when some legend items are not displayed? My dynamic chart has a legend with dynamic area size. So depending on amount of series and the legend are, some legend entries may not be shown (no space for them). I just want to add a warning to my user in such a case. I have tried :

ActiveChart.Legend.LegendEntries(x).Top compared to (ActiveChart.Legend.Top + ActiveChart.Legend.Height)
but ActiveChart.Legend.LegendEntries(x).Top for items not displayed did not give decisive correlation with visibility. Did see some random values >80 000 but then others that were similar to that of my displayed items.
Any help welcome.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I think you could query the .Top of each legend entry. The top of the first legend entry starts at the top of the legend, and the top of the last visible legend entry is above the top+height of the legend. The first non-visible legend entry has a top value that is below the top+height of the legend. And after that, the top property is strange, but I think we can ignore any after the last visible one.

I did the following in the Immediate Window to see the top and bottom (top + height) of the legend, and then the top of each legend entry, and I have indicated the last visible and first not visible legend entries.

Code:
?activechart.legend.top, activechart.Legend.Top + activechart.legend.height
 32.6693700787402            201.420551181102 

for each le in activechart.Legend.LegendEntries : ? le.top : next
 35.6692913385827 
 52.5444094488189 
 69.4195275590551 
 86.2946456692913 
 103.169763779528 
 120.044881889764 
 136.92 
 153.795118110236 
 170.670236220472 
 187.545354330709 <-- last visible
 204.420472440945 <-- first not visible
 32.6692913385827 
 112604.734173228 
 54992.9464566929 
 32.6692913385827 
 32.6692913385827

Note, if you run this code:

Code:
ActiveChart.Legend.Top = 0
ActiveChart.Legend.Height = ActiveChart.ChartArea.Height - 4

the legend will stretch to about the whole height of the chart, and Excel will squeeze in a lot more legend entries, maybe all of them.
 
Upvote 0
It's not a perfect approach.

If the legend is a single column of legend entries, you can examine the .Top property of each legend entry. This should increase monotonically for each subsequent legend entry, because each is lower then the previous one.

When you come to a legend entry whose .Top is less than the previous one, then the previous one is the first one not to be displayed.

Alternatively, starting with the second legend entry (ignore the first), find the first legend entry with a .Top property is less than the .Top property of the legend itself. Then the legend entry before this one is the first not to appear.

I have only been considering legend entries which do not appear because the legend is not large enough to display them all. You can also have legend entries that don't show up because they have been deleted. That's merely another complication!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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