Chart Legend placement

roc_on_the_rocks

Board Regular
Joined
Jun 6, 2009
Messages
175
Office Version
  1. 365
Platform
  1. Windows
I’m facing an unexpected problem positioning the legend on a scatter plot chart (Excel 2007).

Once the chart is plotted (through VBA), the left side of the chart legend overlaps with secondary Y-axi labels. So I’ve been trying to move the chart legend further to the right, against the chart area border.

My surprise is that when moving the chart legend with VBA code, I cannot get it closer than about 3/8 inch between the legend and chart border. But if I manually move it, I can get it touching the right-side area border.

Code:
    ActiveChart.Legend.Left = 500
    ActiveChart.Legend.Width = 150
An alternative to resolve the overlapping issue is to make the actual plot area smaller (narrower), but I’m avoiding that because the smaller plot area makes is harder to read it.

Any help will be greatly appreciated. Thanks in advance!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Have you tried

Code:
  ActiveChart.Legend.Position = xlLegendPositionRight
  ActiveChart.Legend.Left = ActiveChart.ChartArea.Width
 
Upvote 0
Thanks Mungyo.

Your code:
Code:
ActiveChart.Legend.Position = xlLegendPositionRight
ActiveChart.Legend.Left = ActiveChart.ChartArea.Width
... does place the chart legend at the desired location: against the right-side area border.

However, the legend moves away from the area border if I define legend width narrower than 177.94 (which is the width if I don't state it).

The ActiveChart.Legend.Width and ActiveChart.Legend.Left are overriding each other, depending on the order they are stated on the VBA code:
If I first define Legend Position (to ActiveChart.ChartArea.Width), and then Legend Width (to 160), the Width 'wins' and chart legend moves away from the border.
If I first define Legend Width (to 160), and then Legend Position (to ActiveChart.ChartArea.Width), the Position 'wins' and the Width is overridden (becomes 177.94).
Any idea on how to resolve it?
 
Upvote 0
Hmm. Puzzling. How about :

Code:
ActiveChart.Legend.Position = xlLegendPositionRight
ActiveChart.Legend.Width = 160
ActiveChart.Legend.Left = ActiveChart.ChartArea.Width
 
Upvote 0
Solution

Forum statistics

Threads
1,215,327
Messages
6,124,290
Members
449,149
Latest member
mwdbActuary

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