Legend Members

Use the Legend object to format or move a legend or to get the individual entries from the legend. Use the Chart object’s Legend property to get a reference to this collection. The Legend object has the following members. Key members (shown in bold) are covered in the following reference section:

Application

AutoScaleFont

Border

Clear

Creator

Delete

Fill

Font

Height

Interior

Left

LegendEntries

Name

Parent

Position

Select

Shadow

Top

Width

 

Be sure to set the Chart object’s HasLegend property to True before using this object. The following code moves a chart’s legend to the left side and sets its background color:

Sub LegendMembers( )
    Dim chrt As Chart, lg As Legend
    ' Get the chart.
    Set chrt = ActiveChart
    ' Make sure chart has a legend.
    chrt.HasLegend = True
    ' Get the legend.
    Set lg = chrt.Legend
    ' Set the position.
    lg.Position = xlLegendPositionLeft
    ' Change the background color.
    lg.Interior.Color = &HFFFF00
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.