Legend entry indexing for stacked column chart in Excel 2010

AlisonS

New Member
Joined
Jul 21, 2011
Messages
17
Some years ago I wrote an Excel routine to add totals to a stacked column chart which adds a zero-value series to the chart and then deletes the legend entry for the new series using, in effect, ActiveChart.Legend.LegendEntries(ActiveChart.SeriesCollection.Count).Delete.

I am now updating this for Excel 2010 and am having a problem because Microsoft have changed the way the legend entries are indexed if the legend is to the right (or left) of the chart: in this situation the above code now deletes the legend entry relating to the first series rather than the last! If, however, the legend is at the bottom of the chart, the above code works fine as the legend entries are indexed in the opposite, and usual, order (ie, LegendEntries(1) relates to SeriesCollection(1) etc).

If the legend is in its automatic position, then I can read this and react accordingly, but unfortunately my users cannot be relied upon to leave things alone, so I was hoping to find a property to at least tell me the orientation of the legend, regardless of where it actually is, but I can't find one.

I have read Jeffrey Smith's thread on Removing Legend Entries with VBA but, unfortunately, his code also deletes the wrong legend entry in the above scenario.

Any suggestions as to how I can get round this will be much appreciated.

(I first posted this question to answers.microsoft.com, but have had no responses as yet.)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
v = Library.getAttribute("aria-posinset");

The way to use these in VBA can be found under Scripting on each of the pages.
 
Upvote 0
I did actually try the code, replacing 'object' with ActiveChart.Legend as that is the object I want the property for, but I got the error 'Object doesn't support this property or method' and I can't find a relevant object library to reference.

I've now tried your version but 'library' is not a vba object or keyword.

Having read what limited information I can find online about this property it sounds like it would just give me the legend's position, anyway, not it's orientation.

If you still think this property will provide me with the information I need, could you please give me more details about how to use it.

Thanks.
 
Upvote 0
I don't know why moonfish thought that the "ARIA" (Accessible Rich Internet Applications) framework would be relevant to an Excel chart.

I've encountered the same inconsistency in Excel 2007 charts with my own work. I tried the following in 2007, not in 2010, but most things about charting that are unexpected in 2010 are also unexpected in 2007.

If the legend is in one of its built-in positions (top, left, bottom, right, top right corner), the legend entry ordering is consistent if not logical. The position is the chart.legend.position property.

In top and bottom position, the legend entries are arranged in rows, and numbering goes from left to right, the same as the order you added them to the chart. To delete the last one you added, you delete the one with the highest index as you are accustomed to doing.

In the left, right, and top right corner positions, the legend entries are arranged in columns, and are numbered top to bottom, in reverse order of how you added them. To delete that last one you added, delete legend entry 1.

So far so good. Now for the bad news: in the custom position (i.e., the user has dragged the legend somewhere and possibly resized it to change its orientation), the order of legend entries is the same as entries in the most recent built-in position the legend was in. So depending on how many times your users changed the position of the legend before dragging it around, you will not have any idea what order the legend entries are in.

All is not lost. You can do a select case on the legend position, if it's built-in, you know the order. If it's custom, get the legend's top and left properties, move the legend to a built-in position, delete the known entry, then move the legend back to its custom left and top position. You'll probably have to get the left, top, width, and height of the plot area before moving the legend, and set them back when the legend is returned.
 
Upvote 0
Thank you for a reply that actually makes sense to me! :)

I don't know why moonfish thought that the "ARIA" (Accessible Rich Internet Applications) framework would be relevant to an Excel chart.

I wondered the same thing, but I assumed that moonfish knew something I didn't. Spent quite a bit of time looking into it...

All is not lost. You can do a select case on the legend position, if it's built-in, you know the order. If it's custom, get the legend's top and left properties...

Yes, that's the conclusion I'd come to as well, failing any other suggestions. So, thanks for the confirmation - at least I can stop looking now.

Alison
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

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