title: Excel - count days in each month between two dates
body: <p>I am trying to write an excel formula which can be dragged across a row of cells to give the number of days in each month between two specified dates.  E.g:</p>
  
  <pre><code>         A           B         C       D       E       F 
  
  1  | START DATE | END DATE |  Jan  |  Feb  |  Mar  |  Apr  |...
     ---------------------------------------------------------
  2  |  10/02/17  | 15/03/17 |   0   |  19   |  15   |   0   |...
  </code></pre>
  
  <p>An added complication is that  the end date may be left blank indicating that the end date should be treated as today. I can put a formula in the cells starting from C2 but columns A &amp; B must be left for user entry.</p>
  
  <p>I made some progress using lots of nested if statements  but am having real trouble getting it to handle if the end date is blank.</p>
  
  <p>Can anyone help with this please?</p>
  
  <p>*Edit</p>
  
  <p>I refrained from showing the formula I had written so far as I'm afraid it's a bit messy but as it has been requested, I have now shown below (this formula from C2)</p>
  
  <pre><code> =IF(AND($A2&lt;C$1,$B2&lt;D$1),DAY(A2),IF($A2&gt;=D$1,0,IF(AND($A2&lt;C$1,$B2=""),IF(TODAY()&lt;C$1,0,IF(TODAY()&gt;C$1,IF(TODAY()&lt;D$1,DAY(TODAY()),D$1-C$1),IF(TODAY()&lt;D$1,INT(TODAY()-$A2),$D1-$C1))),IF(AND($A2&gt;=C$1,$B2=""),IF(TODAY()&lt;D$1,INT(TODAY()-$A2+1),D$1-$A2),IF(AND($A2&gt;=C$1,$B2&lt;D$1),$B2-$A2+1,IF(AND($A2&lt;C$1,$B2&lt;C$1),0,IF(AND($A2&gt;=C$1,$B2&gt;=D$1),D$1-$A2,IF(AND($A2&lt;C$1,$B2&lt;D$1),DAY(B2),IF(AND($A2&lt;C$1,$B2&gt;=D$1),D$1-C$1,0)))))))))
  </code></pre>