first i would like to say thank you for the workbook and very clear question

second, try this:

=IF(AND($E2<=G$1,$F2>EOMONTH(G$1,-1)),IF($F2>G$1,G$1,$F2)-IF($E2<=EOMONTH(G$1,-1),EOMONTH(G$1,-1)+1,$E2)+1,0)