vba add legend to chart in Excel

Q180. In Excel, how can I add a legend to a chart using VBA?

For example, I already have a chart ("Chart1") of Sales data for Q1-2016 and Q2-2016. I want to add a Legend to the chart using VBA?

excel vba add legend to chart

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Sub AddChartLegend()
  3. ActiveSheet.ChartObjects("Chart1").Chart.HasLegend = True
  4. ActiveSheet.ChartObjects("Chart1").Chart.Legend.Position = xlBottom
  5. End Sub

Description:

a) Line 3 adds a Legend to chart.

b) Line 4 defines the position of chart (xlTop, xlBottom, xlLeft, xlRight, xlCorner)

c) Similarly, if Data Labels hves to be added, the command becomes, ActiveSheet.ChartObjects("Chart1").Chart.SeriesCollection(1).HasDataLabels = True (This is for Series collection 1 - for Series collection 2, the argument changes to 2).

Result after Macro execution:

excel excel vba add legend to chart

 

You can find similar Excel Questions and Answer hereunder

1) How can I get the count of number of series in a Chart using VBA?

2) How can I dynamically add a hyperlink using VBA?

3) How can I list all files in a folder using VBA?

4) How to add a link in a sheet to another sheet

5) How can I change the Marker size and Marker line color for all the series in a chart?

6) How can I check if a file exists in a folder using VBA?

7) How do I add a symbol like Triangle / Inverted Triangle for indicating trends in a cell using VBA?

8) How can I get users to select a file for processing using my macro?

9) Userform initialize vs userform show in Excel

10) How can I loop through all WorkSheets and get their names using VBA?

11) Vba length of an array in Excel

12) How can I find the number of working days between 2 dates using VBA?

13) How can I get input from user through a prompt and assign user's input to a cell?

14) How to change desktop background in Excel

15) How can I set up ListBox using VBA to allow users to select multiple values?