Sheet formula to calculate maximum gap between dates

arpd123

New Member
Joined
May 14, 2018
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi all. I have the following problem:

I have a table and for each record there are multiple date fields. Here is a brief example (these dates use the British format of Day/Month/Year):

Date1Date2Date3Date4Date5Date6
06/09/2017
03/11/201706/10/201728/09/2017
13/11/2017
26/04/201803/04/201802/05/201802/05/2018
20/04/2018
05/02/201805/02/201806/03/2018
14/12/2017
08/09/201719/02/2018
03/05/201826/04/201806/04/201802/05/201802/05/2018
23/04/201802/04/2018
08/09/201702/04/2018
26/04/201803/04/201802/05/201802/05/2018
01/04/201818/10/2016

<tbody>
</tbody>

I would like a sheet formula which will enable me to find the size of the largest gap in "consecutive" dates in each record.

As you can see, the data are entered haphazardly. e.g. it cannot be assumed that Date2 is after Date1. It may in fact precede it. If Date1 is not filled, it does not mean that a subsequent date field is not also filled. The formula has to remove blanks from consideration.

Taking the example of the second row in the above data, the largest gap is between Date1 and Date2 (28 days), and this is the required answer. Between Date2 and Date 3 in the same row it is 8 days. The gap between Date1 and Date3 is larger at 36 days, but does not count because Date2 is intermediate between them. You can assume that the header Date1 is in cell A1.

Thanks for any help on this.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I'm not sure if your example is representative of your full data so I've assumed you stop at Date 6 (the formula would get unwieldy beyond that). For row 3 the formula would be:

=MAX(IF(AND(A3>0,B3>0),ABS(A3-B3),0),IF(AND(B3>0,C3>0),ABS(B3-C3),0),IF(AND(C3>0,D3>0),ABS(C3-D3),0),IF(AND(D3>0,E3>0),ABS(D3-E3),0),IF(AND(E3>0,F3>0),ABS(E3-F3),0))

Use of ABS means dates can be before or after each other.
 
Upvote 0
I'm not sure if your example is representative of your full data so I've assumed you stop at Date 6 (the formula would get unwieldy beyond that). For row 3 the formula would be:

=MAX(IF(AND(A3>0,B3>0),ABS(A3-B3),0),IF(AND(B3>0,C3>0),ABS(B3-C3),0),IF(AND(C3>0,D3>0),ABS(C3-D3),0),IF(AND(D3>0,E3>0),ABS(D3-E3),0),IF(AND(E3>0,F3>0),ABS(E3-F3),0))

Use of ABS means dates can be before or after each other.
Thank you for the response. However, it is not giving the correct answer in general as you seem to have assumed that the dates Date1-Date6 are ordered chronologically in either direction, but I am afraid they are not.

I thought about the comparisons route, but it is as you say rather unwieldy. I think you have to consider all fifteen possible comparisons in each row, and eliminate at least ten of those, depending on how many values are actually filled in each row, and whether the two values being compared have an intermediate value between them elsewhere in the row.

I was wondering if there is a technique with array formulas to work on an intermediate, sorted array of values in the range A3:F3, say.
 
Upvote 0
The formula only needs to check consecutive dates in my interpretation of what you wanted. As I stated, I've included the ABS function so that the chronological order doesn't matter. The results I get from the formula I developed are as follows:

RowAnswer
20
328
40
529
60
70
80
90
1026
110
120
130
1429
150

<colgroup><col><col></colgroup><tbody>
</tbody>

If these aren't the answers you're expecting, please advise the correct ones and it may help to clarify your question.
 
Upvote 0
The formula only needs to check consecutive dates in my interpretation of what you wanted. As I stated, I've included the ABS function so that the chronological order doesn't matter. The results I get from the formula I developed are as follows:

If these aren't the answers you're expecting, please advise the correct ones and it may help to clarify your question.
I can see that there is some ambiguity, my apologies. I do not mean by "consecutive" that e.g. Date1 and Date2 are necessarily consecutive. I mean instead the harder problem of finding the biggest gap if the dates in each row were to be arranged chronologically.
 
Last edited:
Upvote 0
OK, I think I see where you're coming from now. In that case I think it's too complicated with standard Excel functions and you would be better off writing a custom VBA function to loop through all the permutations.
 
Upvote 0
If you're allowed to use VBA then this should do the trick. If not then it may help someone else :)

Usage example: =MaxDateDifference(A10:F10)

Code:
Function MaxDateDifference(rng As Range)

Dim i As Long

Dim cell As Range
Dim myArray() As Long
Dim DateDifference As Long

' Resize array to match number of items in selected range
ReDim myArray(0 To rng.Count - 1)

' Populate array
i = 0
For Each cell In rng
    myArray(i) = cell
    i = i + 1
Next cell

' Sort array into date order
Call QuickSort(myArray, LBound(myArray), UBound(myArray))

' Identify largest date difference in array
DateDifference = 0

For j = 1 To UBound(myArray)
    If myArray(j) - myArray(j - 1) > DateDifference And myArray(j - 1) > 0 Then
        DateDifference = myArray(j) - myArray(j - 1)
    End If
Next j

MaxDateDifference = DateDifference

End Function

Sub QuickSort(arr, Lo As Long, Hi As Long)
  Dim varPivot As Variant
  Dim varTmp As Variant
  Dim tmpLow As Long
  Dim tmpHi As Long
  tmpLow = Lo
  tmpHi = Hi
  varPivot = arr((Lo + Hi) \ 2)
  Do While tmpLow <= tmpHi
    Do While arr(tmpLow) < varPivot And tmpLow < Hi
      tmpLow = tmpLow + 1
    Loop
    Do While varPivot < arr(tmpHi) And tmpHi > Lo
      tmpHi = tmpHi - 1
    Loop
    If tmpLow <= tmpHi Then
      varTmp = arr(tmpLow)
      arr(tmpLow) = arr(tmpHi)
      arr(tmpHi) = varTmp
      tmpLow = tmpLow + 1
      tmpHi = tmpHi - 1
    End If
  Loop
  If Lo < tmpHi Then QuickSort arr, Lo, tmpHi
  If tmpLow < Hi Then QuickSort arr, tmpLow, Hi
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,786
Members
449,125
Latest member
shreyash11

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