One way to do this is to dispense with helper columns and just have a single formula in AA9:AA20, i.e. this "array formula" in AA9 confirmed with CTRL+SHIFT+ENTER and copied down
=SUM(IF(F$2:F$34<X9,0,IF(E$2:E$34>EOMONTH(X9,0),0,IF(F$2:F$34=E$2:E$34,1,IF(F$2:F$34>EOMONTH(X9,0),EOMONTH(X9,0)+1,F$2:F$34)-IF(E$2:E$34<X9,X9,E$2:E$34)))))
Now if you have this formula in G2 copied down
=IF(E2=F2,1,F2-E2)
Your totals in G37 and AA21 should match (assuming all dates are within 2012) - see attached
Bookmarks