问题
Struggling to come up with a formula that works for this (Data in A1 - E4)
StartDate EndDate Jan Feb Mar
12/4/2018 4/20/2019 31 28 31
9/26/2018 1/30/2019 30 0 0
1/1/2019 3/31/2019 31 28 31
1/1/2015 3/31/2019 155 141 155
IN Cell C2, formula is:
=SUMPRODUCT(--(TEXT(ROW(INDIRECT($A2 & ":" & IF($B2="",TODAY(),B2))),"mmm")=C$1))
What I am trying to do is exclude past dates from the sum, for example in the 4th row, the Jan/Feb/Mar should be 31/28/31 and not count previous years.
Any ideas where I am going wrong?
回答1:
Add a chck for the year:
(YEAR(ROW(INDIRECT($A2 & ":" & IF($B2="",TODAY(),$B2))))=YEAR(TODAY()))
and just multiply them in the SUMPRODUCT:
=SUMPRODUCT((YEAR(ROW(INDIRECT($A2 & ":" & IF($B2="",TODAY(),$B2))))>=YEAR(TODAY()))*(TEXT(ROW(INDIRECT($A2 & ":" & IF($B2="",TODAY(),$B2))),"mmm")=C$1))
One Note:
INDIRECT is Volatile and will re-calc each time Excel re-calcs. I prefer to replace them with INDEX:
=SUMPRODUCT((YEAR(ROW(INDEX($A:$A,$A2) :INDEX($A:$A, IF($B2="",TODAY(),$B2))))>=YEAR(TODAY()))*(TEXT(ROW(INDEX($A:$A,$A2) :INDEX($A:$A, IF($B2="",TODAY(),$B2))),"mmm")=C$1))
来源:https://stackoverflow.com/questions/54012017/formula-to-calculate-days-by-month-between-two-dates-for-future-periods-only