In our daily work, we usually have the problem of counting the average of a period. If the given dates are continuous, we can handle this situation very well, and if the dates are discontinuous, how do we count the average for different periods? In this article, we will use some examples to show you how to count the average for two dates by Average formula in different situations.
Table of Contents
1. Count the Average Between Continuous Dates
First prepare a table with given dates and sales.
You can see the given dates are continuous, so we can count average by Average formula directly.
Step1: In B7, enter the below formula to count the average between dates 1/28/2019
and 3/28/2019
.
=AVERAGE(B2:B4)
Step2: Click Enter to get the result.
2. Count the Average Between Discontinuous Dates using Formula
If in above table the dates are discontinuous and unordered, how can we count the average of sales? See screenshot below.
You can follow below steps to count the average for two discontinuous dates.
Step1: Prepare another table to save the Start Date, End Date and Average.
Step2: In Start Date enter the start date of the period you want to count the average. In End Date enter the end date of the period you want to count the average. For example, we count the average for period 2/28/2019
and 4/28/2019
.
Step3: In Average, enter the formula:
=AVERAGE(IF((A2:A7>=B10)*(A2:A7<=B11),B2:B7))
In above formula, A2:A7 is the given date range, B10 is the start date, B11 is the end date, B2:B7 is the sales range.
Step4: After entering the formula, click Ctrl + Shift + Enter
to get the result.
Check the formula bar, you can find the formula is displayed as below. That’s an array formula.
Notes:
1. In step 4, if we just click Enter after entering the formula, then we will get an error here. That’s because above formula is an array formula.
2. If we just want to use a normal formula to count the average and click Enter to get the result, we can use below formula:
=SUMPRODUCT(--(A2:A7>=B10),--(A2:A7<=B11),B2:B7)/SUMPRODUCT(--(A2:A7>=B10),--(A2:A7<=B11))
We can get the same result.
3. Count the Average Between Discontinuous Dates using User Defined Function with VBA Code
You can also use a User Defined Function with VBA Code to count the average between two given dates in Excel. Just follow these steps:
Step1: Open the Visual Basic Editor (VBE) by pressing Alt + F11 or going to the Developer tab and clicking on Visual Basic.
Step2: Insert a new module by going to the Insert menu and selecting Module.
Step3: Write your user defined function in the module using the Function keyword and the desired name, parameters and return value. Save and close the VBE.
Function MyAverage_Excelhow(rng1 As Range, rng2 As Range) As Double
Dim sum As Double
Dim count As Long
Dim i As Long
Dim crit1 As Variant
Dim crit2 As Variant
' Check if the ranges have the same size
If rng1.Rows.count <> rng2.Rows.count Or rng1.Columns.count <> rng2.Columns.count Then
MyAverage = CVErr(xlErrValue) ' Return a #VALUE! error
Exit Function
End If
' Get the criteria from cells B10 and B11
crit1 = ActiveSheet.Range("B10").Value
crit2 = ActiveSheet.Range("B11").Value
' Loop through the ranges and calculate the sum and count of the values that meet the criteria
For i = 1 To rng1.Cells.count
If rng1.Cells(i).Value >= crit1 And rng1.Cells(i).Value <= crit2 Then
sum = sum + rng2.Cells(i).Value
count = count + 1
End If
Next i
' Calculate and return the average
MyAverage_Excelhow = sum / count
End Function
Note: you need to change both two criterion variables in cell B10, B11 as you need.
Step4: Go back to Excel and enter a cell where you want to use the function. Type the following formula:
=MyAverage_Excelhow(A2:A7,B2:B7)
Step5: press Enter to apply this formula. And the average result will be returned.
4. Video: Count the Average Between Two Dates
This video will show you how to count the average between two dates in Excel using a simple formula or a VBA code.
5. Related Functions
- Excel SUMPRODUCT function
The Excel SUMPRODUCT function multiplies corresponding components in the given one or more arrays or ranges, and returns the sum of those products.The syntax of the SUMPRODUCT function is as below:= SUMPRODUCT (array1,[array2],…)… - Excel IF function
The Excel IF function perform a logical test to return one value if the condition is TRUE and return another value if the condition is FALSE. The IF function is a build-in function in Microsoft Excel and it is categorized as a Logical Function.The syntax of the IF function is as below:= IF (condition, [true_value], [false_value])…. - 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],…)….