You're welcome!
I'm sure you could figure it out. It's just practice and the list of available functions:
Excel functions (alphabetical)
Excel holds dates as an integer since 1 Jan 1900. If I take today's date and add 1 I'll get tomorrow's date.
EOMONTH (end of month) has two parameters, a start date and a number of months to offset. It gives the end of the specified month (e.g. =EOMONTH(TODAY(),-1)+1 takes the current date, gives the last day of the previous month (31-Oct-2019) and the +1 is plus 1 day so the result is 1-Nov-2019)
IF has three parameters, IF(condition, result if true, result if false).
Your cells for counting days have 4 options:
- This month is the same as Start Date so count days from Start Date to end of month.
- This month is the same as End Date so count days from start of previous month to End Date.
- This month is between Start Date and End Date so count number of days in this month.
- This month is not a part of or between either Start Date or End Date so return zero.
So to deconstruct =IF(EOMONTH($A2,0)=EOMONTH(C$1,0),EOMONTH($A2,0)-$A2+1,IF(EOMONTH($B2,0)=EOMONTH(C$1,0),$B2-EOMONTH($B2,-1),IF(AND(C$1>$A2,C$1<$B2),EOMONTH(C$1,0)-EOMONTH(C$1,-1),0)))
(1). IF(EOMONTH($A2,0)=EOMONTH(C$1,0),EOMONTH($A2,0)-$A2+1 says if the last day of the month of the Start Date is the same as the heading month then get the date of the end month of the Start Date, subtract the Start Date and add 1.
(2). IF(EOMONTH($B2,0)=EOMONTH(C$1,0),$B2-EOMONTH($B2,-1) says if the last day of the month of the End Date is the same as the heading month then subtract the last day of the previous month of the End Date from the End Date.
(3). IF(AND(C$1>$A2,C$1<$B2),EOMONTH(C$1,0)-EOMONTH(C$1,-1) says if the heading date is greater than the Start Date AND the heading date is less than the End Date then return the end date of the current month minus the end date of the previous month of the heading (i.e. number of days in that month).
(4). ,0) says if none of the previous nested IF statements is true then return zero.