Hi,
I have a sheet which contains arrival dates in column A and departure dates in column B.
In cell column D, I have dates representing the first day of the month. For each month, I want to calculate the number of nights stayed based on all date ranges in columns A and B for the month as specified in column D. In the example below, June should be 33 and July 43.
| Arrival |
Departure |
|
Date |
Nights in period |
| 08/06/2024 |
08/07/2024 |
|
01/06/2024 |
0.00 |
| 03/06/2024 |
07/06/2024 |
|
01/07/2024 |
0.00 |
| 25/05/2024 |
26/05/2024 |
|
|
|
| 24/06/2024 |
27/06/2024 |
|
|
|
| 17/06/2024 |
21/06/2024 |
|
|
|
| 01/07/2024 |
05/07/2024 |
|
|
|
| 25/07/2024 |
29/07/2024 |
|
|
|
| 19/07/2024 |
21/07/2024 |
|
|
|
| 21/07/2024 |
25/07/2024 |
|
|
|
| 05/07/2024 |
26/07/2024 |
|
|
|
| 30/07/2024 |
03/08/2024 |
|
|
|
All date cells are formatted as Excel 'Date' with the UK display format of dd/mm/yyyy.
The following formula returns zero.
=SUMPRODUCT(MAX(0, MIN(EOMONTH(D2,0), B:B) - MAX(D2, A:A)))
It returns the correct result when there is only one row of dates (E.G. delete all data from row 3 down), although I do need to correct this at some point as I was expecting 23 and 7 (separate issue).
| Arrival |
Departure |
|
Date |
Nights in period |
| 08/06/2024 |
08/07/2024 |
|
01/06/2024 |
22.00 |
|
|
|
01/07/2024 |
7.00 |
The component parts return their expected values (MIN, MAX, EOMONTH). Even with only two rows of date where there are no overlaps, I still get zero.
| Arrival |
Departure |
|
Date |
Nights in period |
| 08/06/2024 |
08/07/2024 |
|
01/06/2024 |
0.00 |
| 03/06/2024 |
07/06/2024 |
|
01/07/2024 |
0.00 |
I've also tried constraining the range to just those with dates (E.G. B2:B12 rather than B:B) which does not work either.
Can someone please help me work out why this fails for more than one row?
Thanks
Carl Gilbert