VBA Legend and Resizing Parcel

I have an Excel chart that changes when selected in slicers. I noticed that the plot area and the legend area change depending on the selection made.

I tried to fix the position and size of the plot area using vba, but that unfortunately doesn't work.

Area and legend are preserved across resize, causing the legend to overlap the plot area. Which I obviously don't want.

I have this code placed in a vba editor worksheet page:

Option Explicit
Private Sub Chart_Calculate()

ChartObjects("grafiek 4").Activate
ActiveChart.PlotArea.Width = 637.783
ActiveChart.Legend.Left = 716.514
ActiveChart.Legend.Width = 176.735
ActiveChart.Legend.Height = 295.334

End Sub

      

with this code, I assumed the auto-resizing would disappear, but I saw that the legend sometimes still overlaps the plot area.

Is there a solution that permanently fixes this issue?

Edit1:

Yesterday I just added a few parameters for the plot area. It seemed to work. But now I tried again and the legend overlaps the plot area again.

I changed the code to:

Option Explicit
Private Sub Chart_Calculate()

ChartObjects("grafiek 4").Activate
ActiveChart.PlotArea.Top = 33.102
ActiveChart.PlotArea.Left = 67.1
ActiveChart.PlotArea.Width = 637.783
ActiveChart.Legend.Top = 7
ActiveChart.Legend.Left = 716.514
ActiveChart.Legend.Width = 176.735
ActiveChart.Legend.Height = 329.667

End Sub

      

So, 2 more parameters for the plot area.

edit2: I checked the legend properties in Excel. there is a checkbox under "Options for Legend": Show legend without overlapping plot area (I don't know the exact English text). This checkbox is checked, but it overlaps the plot area.

Why is this impossible to achieve? Fixed dimensions for lot area and legend shouldn't be that hard.

edit 3: I currently have this routine in my book:

Option Explicit
Private Sub Chart_Calculate()

ChartObjects("grafiek 4").Activate
With ActiveChart
    With .PlotArea
        .Top = 33.102
        .Left = 67.1
        .Width = 637.783
    End With
    With .Legend
        .IncludeInLayout = True
        .Position = xlLegendPositionRight
        .AutoScaleFont = False
        .Font.Size = 8
        .Top = 5
        .Left = 706.899
        .Width = 179.735
        .Height = 336.681
    End With
End With

End Sub
Sub kopieergrafiek()

ActiveSheet.ChartObjects("Grafiek 4").Copy

End Sub

      

(including a suggestion in a comment below my post)

I don't seem to be working. Is it possible that workheet_change performs better?

edit 4: I still don't have a solution for this problem. This already happens when name 1 of the legend items is longer to fit the space. And this also happens when there are many items in the legend that can be placed in the available space.

I think there is no solution for this. If I couldn't tell excel somehow to increase the number of items in the legend. or to maximize the length of the series name.

+4


source to share


2 answers


I had this problem myself with a legend resizing plot area. I tried what Portland Runner suggested, only setting .Legend.IncludeInLayout to false (thus decoupling the legend from the plot area as he suggested, maybe he made a typo?) And my plot area didn't change anymore.



+2


source


I had this problem too and found this answer. I found a fix that works for me. Not sure why exactly this works, but I am doing these steps:



  • Set Legend Position
  • Disable legend (legend.includeLayout = false)
  • Resize the patch to the required size
  • Reinstall the legend (legend.includeLayout = True)
  • Set the Legend.Left position and then the legend is correctly positioned and lined up.
0


source







All Articles