VBA have legend on the top left corner of chart area

absundo

New Member
Joined
Jan 30, 2015
Messages
23
I have multiple charts with the same series, so I want to write a code where I can just have 1 legend instead of having a legend per chart.

I felt the easiest way was to write a code where I can have the legend cover the entire chart area. However, i've run into a problem where the legend doesn't want to overlap the plot area.

Code:
With ChartObject.Chart
       If ChartObject.Name = "Legends" Then
          With ChartObject.Chart.Legend
                  .top = ?
                  .left = ?
                 .Width = chartwidth
                 .Height = chartheight
                 .Interior.ColorIndex = 2
                 .Font.Size =5
           Else
                 .HasLegend = False
        End If
End With

I've played around with different top/left combinations and still seem to have the problem where the legend doesn't want to overlap the plot area. Is there a way, to have the legend position it self automatically to the top left corner of the chart area, and then set the width/height of the legend to the chart area?

Any help/input would be appreciated!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi

I added a chart, selected it and this code worked for me.

Code:
Sub TestLegendOverlap()
Dim cht As Chart

Set cht = ActiveChart
With cht.Legend
     .Top = 0
     .Left = 0
    .Width = cht.ChartArea.Width
    .Height = cht.ChartArea.Height
    .Interior.ColorIndex = 2
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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