Array formula to compare values between two dates

Graemea

Board Regular
Joined
Oct 30, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Hi,


I have monthly sales data for two products arranged as follows:


Column A: Consecutive month-end dates (e.g. 31/12/2018, 31/01/2019, 28/02/2019 etc)


Column B: Monthly sales values for Product A


Column C: Monthly sales values for Product B


I would like to create a formula that will calculate the % of months in a stipulated period for which the sales value of Product A was greater than the sales of Product B.


For example, in 5 years (60 months), the sales of Product A was greater than that of Product B in 36 months, so the % returned by the formula is 60% (i.e. 36/60).


I would also like this formula to take into account a start and end date in cells D1 and E1, which will define the period for which the % is to be calculated (e.g. if D1 is 31/12/2017 and E1 is 31/12/2018, the % will be calculated over that 12 month period).


Can someone please suggest a formula that will accomplish all of this?


Thanks!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi Graemea,

If that can be done with a formula, I don't know how. It certainly can be done with VBA though. I set up a sheet as you described:

Excel 2007 32 bit
ABCDEFGH
1WE DateProduct AProduct B30/9/201931/10/2020
231/12/20186066761152
331/1/20194399652628
428/2/2019656336180
531/3/20196743165401
630/4/20196253031532
731/5/20192513058111
830/6/20192116120392
931/7/20197295329917
1031/8/20194534740353
1130/9/20194708819899
1231/10/20191200731616
1330/11/20192034763457
1431/12/20196444530394
1531/1/20205134342754
1629/2/20201791240385
1731/3/20204238256656
1830/4/20201187648569
1931/5/20203708468953
2030/6/20201999871211
2131/7/20206403650609
2231/8/20206935945469
2330/9/20204978734811
2431/10/20201097924733
2530/11/20204815270693

<tbody>
</tbody>
Sheet1
This VBA code will put your result in cell G1:
(This code assumes that values entered in your "start date" and "end date" cells will be month ending dates)

Sub calculate()
Dim startdate As Date, enddate As Date, x As Integer, y As Integer, z As Integer, count As Integer, lrow As Long

startdate = Range("D1").Value
enddate = Range("E1").Value
x = 2
count = 0
lrow = Cells(Rows.count, 1).End(xlUp).Row

Do While Cells(x, 1).Value <> startdate
If x > lrow Then
GoTo abort
Else
x = x + 1
End If
Loop

y = x + 1

Do While Cells(y, 1).Value <> enddate
If y > lrow Then
GoTo abort
Else
y = y + 1
End If
Loop

For z = x To y
If Cells(z, 2) > Cells(z, 3) Then
count = count + 1
Else
End If
Next z

range("G1").Value = count / ((y - x) + 1)
GoTo endofcode

abort:
MsgBox ("Error: Please check your date range and try again")

endofcode:
End Sub


All the best,
Matt

 
Upvote 0
Using example from The_Macrotect above.
See if this formula does what you want. I included the start and end dates in the formula. If you don't want them include just remove the = sign from the formula.
Excel Workbook
ABCDE
1WE DateProduct AProduct B9/30/201910/31/2020
212/31/20186066761152
31/31/2019439965262842.86%
42/28/2019656336180
53/31/20196743165401
64/30/20196253031532
75/31/20192513058111
86/30/20192116120392
97/31/20197295329917
108/31/20194534740353
11437384708819899
12437691200731616
13437992034763457
14438306444530394
151/31/20205134342754
162/29/20201791240385
173/31/20204238256656
184/30/20201187648569
195/31/20203708468953
206/30/20201999871211
217/31/20206403650609
228/31/20206935945469
239/30/20204978734811
2410/31/20201097924733
2511/30/20204815270693
Sheet
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top