Calculate number of event days in a moving window

demeeder

New Member
Joined
Apr 27, 2015
Messages
45
I am trying build a tool that will calculate the when a total number of event days in a moving 365 day window exceeds a specific number (220).

Suppose I have 5 events. Each event has a start date and an end date. I can easily count the number of days for each event, but can't figure out how to build an alert when I will exceed 220 total days (not consecutive) in a 365 window (that is moving every day). I want to predict when/if I will every hit 220 well before the known event either starts or is completed.

In this example, you will see that I will exceed 220 on 12/29/2021. That's easy because my start date in #1 is 1/1/2021 and the end date is obviously less than 365 days out.

Book1
ABCDEF
112345
2Start1/1/216/4/2110/1/214/5/226/15/22
3End5/1/216/15/2112/29/215/15/2211/15/22
4120118940153
5120131220260413
6>220 days in a 365 window?
7If so, what date crosses 220?
Sheet1
Cell Formulas
RangeFormula
B4:F4B4=B3-B2
B5B5=B4
C5:F5C5=B5+C4


Thanks!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
It wouldn't be impossible to get the result by formula, but it's easier getting it via a User Function.

Fo example, copy this code into a standard module of your vba project:
Code:
Function myEventsInfo(ByRef SDates As Range, ByRef EDates As Range, Optional myThr As Long = 220) As Variant
'https://www.mrexcel.com/board/threads/calculate-number-of-event-days-in-a-moving-window.1206388/
'
'USAGE:
'   =myEventsInfo(StartDatesRange, EndDatesInterval [, Threshold])
'
'Example:   =myEventsInfo($B2:D2;$B3:D3)
'
'Will return 4 info:
'   The Used Days at beginning of the event
'   The Days the last event will last
'   The cumulative Used days at the end of the Event
'   The date that the Threshold will be reached
'
'   The default Threshold is 220, or it can be specified as the third parametre
'
Dim OArr(1 To 4, 1 To 1), I As Long, evDays As Long, J As Long
Dim BLE As Date, BCE As Date, UsedD As Long, dUsedD As Long
Dim dBLE As Date
'
If SDates.Rows.Count > 1 Then myEventsInfo = CVErr(2022): Exit Function
If EDates.Rows.Count > 1 Then myEventsInfo = CVErr(2022): Exit Function
If SDates.Columns.Count <> EDates.Columns.Count Then myEventsInfo = CVErr(2022): Exit Function
'
BLE = SDates.Cells(1, SDates.Columns.Count)
If EDates.Cells(1, SDates.Columns.Count) < BLE Or BLE = 0 Then
    myEventsInfo = Application.WorksheetFunction.Transpose(Array(0, 0, 0))
    Exit Function
End If
'Counting Used Day at beginning last event:
For I = 1 To SDates.Columns.Count - 1
    If SDates.Cells(1, I) <= (BLE - 365) Then BCE = BLE - 365 Else BCE = SDates.Cells(1, I)
    If BCE <= EDates.Cells(1, I) And EDates.Cells(1, I) < BLE Then UsedD = UsedD + EDates.Cells(1, I) - BCE + 1
Next I
OArr(1, 1) = UsedD
evDays = EDates.Cells(1, SDates.Columns.Count) - BLE + 1
OArr(2, 1) = evDays
'
OArr(3, 1) = OArr(1, 1) + evDays
'If Threshold is exceded then refine the calculation:
If (OArr(1, 1) + OArr(2, 1)) >= myThr Then
    For J = 0 To evDays '- 1
''    If J >= (evDays -2) Then Stop
        dUsedD = 0
        dBLE = BLE + J
        For I = 1 To SDates.Columns.Count - 1
            dBLE = BLE + J
            If SDates.Cells(1, I) <= (dBLE - 365) Then BCE = dBLE - 365 Else BCE = SDates.Cells(1, I)
            If BCE <= EDates.Cells(1, I) Then dUsedD = dUsedD + EDates.Cells(1, I) - BCE + 1
        Next I
'
        If (dUsedD + J) >= myThr And OArr(4, 1) = "" Then
            OArr(4, 1) = dBLE - 1
        End If
    Next J
    OArr(3, 1) = dUsedD + OArr(2, 1) - 0
End If
If OArr(4, 1) = "" Then OArr(4, 1) = ""
myEventsInfo = OArr
End Function

Now go to B5 of your worksheet and insert the formula
VBA Code:
=myEventsInfo($B2:B2,$B3:B3)

If your version does support Dynamic Arrays (ex Office 365) you will enter the formula using Enter, and the formula will return 4 rows, filling B5:B8
If your version does NOT support Dynamic Arrays then you need to select B5:B8, copy the formula in the formula bar, then confirm it using Contr-Shift-Enter

The syntax of the function is:
Code:
=myEventsInfo(StartDatesRange, EndDatesInterval [, Threshold])
Threshold is optional, its default value is 220

The 4 rows returned by the function will state:
- The Used Days at beginning of the event
- The Days the last event will last
- The cumulative Used days at the end of the Event
- The date that the Threshold will be reached

Now you may copy B5 to the right to get the information about the other Events; if the formula has been enterd using Contr-Shifr-Enter thyen you have to copy the entire block (B5:B8) and paste to the next columns

HTH
 
Upvote 0
Thanks!! I am out for the weekend but will apply your solution on Monday and provide any feedback then. Much appreciated!!!

Dave
 
Upvote 0
I forgot to attach a sample of results (see image)
 

Attachments

  • EVENT-Days_Immagine 2022-05-28 152312.jpg
    EVENT-Days_Immagine 2022-05-28 152312.jpg
    55.9 KB · Views: 10
Upvote 0
I believe this works well. Grazie!!!

Works for me on my home computer. Will see if the work version of Excel appreciates the VBA.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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