VBA - How to determine the number of legend entries shown

Viewed 100

I've looked all over for the answer to this and I am completely stumped. Any help would be greatly appreciated!

Look at this example: example picture

As you can see, there are 20 .Series in the .Chart, but only 15 are visible in the .Legend. I want to use VBA to adjust the .Legend size and/or reduce the .Legend font size until all 20 .Series are shown in the .Legend.

But I have no idea how to determine the number of .LegendEntries shown in the .Legend.

.Legend.LegendEntries.Count gives me "20" as a result.

I'm at a loss.

1 Answers

Using this code on your example (your results may vary) you can identify which legend entries are not visible. It calculates the difference between the legend's window and the legend entry's top. A negative should indicate it is not visible

        Dim c As Chart, nLegH As Single
        Set c = ActiveChart
        With c.Legend
                nLegH = .Top + .Height
                Debug.Print nLegH
                For x = 1 To 20
                        Debug.Print .LegendEntries(x).Top, nLegH - .LegendEntries(x).Top
                Next x
        End With
Related