Adding a horizontal Average Line to a Chart or Graph

This post explains how to add a horizontal average line to an existing Excel chart or graph in excel. How do I add a horizontal average line to a chart or graph gives loads of context for the viewer. How to use Excel VBA Macro to add a horizontal average line to the existing chart in Excel.

For example, you have a simple Excel spreadsheet tracking product sales. And you have a column with dates and another with sales. And you have made a simple chart with both columns as data sources. And you want to add a horizontal average line to this chart so that the user can see how close they are to their average sales. You can follow this guide to achieve the result.

Adding a horizontal Average Line to a Chart or Graph


To add a horizontal average line to a chart, just do the following steps:

 #1 create a new average line column that contains average sales data. And type the following formula into the first cell of average column. =AVERAGE(B2:D2), then drag the AutoFill Handle over to the average columns.

adding horizontal average line1

#2 Right-click on the existing chart, and choose Select Data… from the drop-down menu list.

adding horizontal average line2

#3 the Select Data source window will appear.

#4 click Add button under Legend Entries section, the Edit Series window will appear.

adding horizontal average line3

#5 enter one Series name such as: Average line into the Series name text box. Then select average line column as its series values. Then click OK button.

adding horizontal average line4

#6 you will see that the average line has been added into the cart.

adding horizontal average line5

#7 Right-click on the newly average line series, and select Change Series Chart Type from the drop down menu list. The Change Chart Type window will appear.

adding horizontal average line6

adding horizontal average line7

#8 Select chart type as line for the average line series.

adding horizontal average line8

So far, the horizontal average line is added into the existing chart.

Adding a horizontal Average Line to a Chart or Graph with VBA


You can also write an Excel VBA Macro to add a horizontal average line to an excel chart, just do the following steps:

#1 click on “Visual Basic” command under DEVELOPER Tab.

Get the position of the nth using excel vba1

#2 then the “Visual Basic Editor” window will appear.

#3 click “Insert” ->”Module” to create a new module.

convert column number to letter3

#4 paste the below VBA code into the code window. Then clicking “Save” button.

adding horizontal average line9

Sub AddAverageLineChart()
    Dim S As Series
    Dim A As Variant
    Dim T As Double
    Dim O As Variant
    If VBA.TypeName(Application.Selection) <> "Series" Then Exit Sub
    Set S = Application.Selection
    A = S.Values
    T = Application.WorksheetFunction.Average(A)
    ReDim O(LBound(A) To UBound(A))
    For i = LBound(O) To UBound(O)
        O(i) = T
    Next
    With ActiveChart.SeriesCollection.NewSeries
    .XValues = S.XValues
    .Values = O
    .Name = "Average line" & S.Name
    .AxisGroup = S.AxisGroup
    .MarkerStyle = xlNone
    .Border.Color = S.Border.Color
    .ChartType = xlLine
    .Format.Line.ForeColor.ObjectThemeColor = msoThemeColorAccent6
    End With
End Sub

#5 select all data of columns in your chart.

adding horizontal average line12

#6 back to the current worksheet, then run the above excel macro. Click Run button.

adding horizontal average line10

#7 Let’s see the result.

adding horizontal average line11

Related Functions


  • Excel AVERAGE function
    The Excel AVERAGE function returns the average of the numbers that you provided.The syntax of the AVERAGE function is as below:=AVERAGE (number1,[number2],…)….

 

Related Posts

VBA Macro For VLOOKUP From Another Sheet
vba macro for vlookup from another sheet1

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you how to use VBA code to vlookup data ...

How To Insert Comments in Protected Worksheet in Excel
insert commetns in protected workshee3

This post will show you how to allow comments in a protected worksheet in Excel. You can easily to insert comments into cells in a normal worksheet in Excel, but if want to insert a comment in a worksheet that ...

How To Convert Text to Upper Cases(Using VBA) in Excel
convert text to upper cases1

This post will show you how to switch from lower case to upper case in Excel. and I am going to show you two different ways of converting text to upper cases using formula or VBA macro in Excel 2013,Excel ...

How To Hide Every Other Row in Excel (Using VBA)
hide every other row1

This post will show you how to hide alternate rows or columns in Excel or how to hide every third, fourth, fifth row or column in Excel. If you want to hide every other row in your current worksheet, how ...

How to Disable the Save As Prompt in Excel
disable save as prompt1

This post will show you how to use a VBA Macro to save an Excel file and overwrite any existing file without a prompt so that you are going to get the little window that says file already exists do ...

How to Count Cells that Contain even or odd numbers in Excel
count cells that contain even numbers5

This post will guide you how to count the number of cells that contain odd or even numbers within a range of cells using a formula in Excel 2013/2016.How do I count cells that contain odd numbers through the use ...

How to Count Cells that Contain negative Numbers in Excel

This post will guide you how to count the number of cells that contain negative numbers within a range of cells using a formula in Excel 2013/2016.You can count the number of negative numbers in your data using easy functions ...

How to Count Cells Are Not Blank or Empty in Excel
count non blank nonempty cells5

This post will guide you how to count cells that are not blank or empty in a given range cells using a formula in Excel 2013/2016.How do I count the number of cells that are not blank in a particular ...

How to Count Cells Less Than a Specific Value in Excel
count cells lessr than5

This post will guide you how to count the number of cells less than a particular numeric value in a given range cells using a formula in Excel 2013/2016. How do I count cells that are less than a specific ...

How to Count Cells Greater Than a Specific Value in Excel
count cells greater than 5

This post will guide you how to count the number of cells greater than a particular numeric value in a given range cells using a formula in Excel 2013/2016. How do I count cells that are greater than a specific ...

Sidebar