First Step
Use Text Join to Concatenate your dates.
Insert this into cell E2 using Ctrl Shift Edit and then fill Down
Formula:
=TEXTJOIN(", ",1,IF(COUNTIF(A$1:A2,A3)>0,"",IF($A$2:$A$21=A3,$B$2:$B$21 &":" &$C$2:$C$21,"")))
That Gives you the List Of Dates for each file.
You can feed that list into a userdefined function using the formula in E2:-
Formula:
=UniqueDates(TEXTJOIN(", ",1,IF(COUNTIF(A$1:A1,A2)>0,"",IF($A$2:$A$21=A2,$B$2:$B$21 &":" &$C$2:$C$21,""))))
Entered using Ctrl Shift Enter
Paste the Userdefined Function in to a Macro Module to make it all work:-
Function UniqueDates(Entry As String)
UniqueDates = 0
If Entry = "" Or InStr(Entry, ", ") = 0 Then UniqueDates = 0: GoTo Quit
MyArray = Split(Entry, ", ")
'Sort Array by First Date
SortLoop:
Flag = False
For Count = 0 To UBound(MyArray) - 1
Dates1 = Split(MyArray(Count), ":")
Dates2 = Split(MyArray(Count + 1), ":")
If Dates1(0) - Dates2(0) > 0 Then
Flag = True
t = MyArray(Count)
MyArray(Count) = MyArray(Count + 1)
MyArray(Count + 1) = t
End If
Next
If Flag = True Then GoTo SortLoop
For Count = 0 To UBound(MyArray) - 1
'Get First 2 Dates
Dates1 = Split(MyArray(Count), ":")
Dates2 = Split(MyArray(Count + 1), ":")
t = Dates1(1) - Dates2(0)
If t >= 0 Then
'There is an Overlap
t = Dates1(1) - Dates2(1)
If t >= 0 Then
'There is a double Overlap
MyArray(Count + 1) = MyArray(Count)
Else
'There is a Single Overlap
MyArray(Count + 1) = Dates1(0) & ":" & Dates2(1)
End If
Else
'No Overlap
UniqueDates = UniqueDates + Dates1(1) - Dates1(0) + 1
End If
Next
UniqueDates = UniqueDates + Dates2(1) - Dates2(0) + 1
Quit:
End Function
This Formula is probably better:-
Formula:
=UniqueDates(TEXTJOIN(", ",1,IF(COUNTIF(A$1:A1,A2)>0,"",IF($A$2:$A$21=A2,INT($B$2:$B$21) &":" &INT($C$2:$C$21),""))))
Bookmarks