[英]excel calculate number of days per month in certain period
I know this has been asked many times.. but I cannot make it work for my scenario.我知道这已经被问过很多次了。但我不能让它适用于我的场景。 I need to calculate number of days occupancy between 2 dates, but I need to consider year as well... (IE. jan-2020 till dec 2021.
我需要计算两个日期之间的入住天数,但我也需要考虑年份...(即 2020 年 1 月至 2021 年 12 月。
My formula for calculations is: =MAX(0; MIN(EOMONTH(C$1; 0); $B2)-MAX(C$1; $A2) +1)
where C1 is month that I am looking for, B2 is end date, A2 is start date...我的计算公式是:
=MAX(0; MIN(EOMONTH(C$1; 0); $B2)-MAX(C$1; $A2) +1)
其中 C1 是我要查找的月份,B2 是结束日期, A2 是开始日期...
here is my screenshot:这是我的截图:
This is results that I am expecting:这是我期待的结果:
I think I am having issue with formatting that I don't know how to overcome... with more testing it looks like my formula was fine in the first place:我认为我的格式有问题,我不知道如何克服......通过更多测试,看起来我的公式一开始就很好:
I selected start date column and end date column than:我选择的开始日期列和结束日期列比:
Data > Text to Columns Next, Next, then select Date 'DMY'.... did the trick. Data > Text to Columns Next, Next, 然后 select Date 'DMY'.... 成功了。
Start and end dates are converted back to dates and everything started working for me....开始和结束日期被转换回日期,一切都开始为我工作......
Dates where imported from database and they had leading space in front of it... that is why calculations failed.从数据库导入的日期并且它们前面有前导空格......这就是计算失败的原因。 Once they are back to real dates everything started working...
一旦他们回到真实日期,一切都开始工作......
Thanks for help guys!谢谢你们的帮助!
I am just posting another IF based variation which I had worked out.我只是发布另一个我已经制定的基于 IF 的变体。
=IF(OR(AND(C$1>$A2,$B2>C$1),AND(EOMONTH(C$1,0)>$A2,$B2>EOMONTH(C$1,0))),MIN($B2,EOMONTH(C$1,0))-MAX($A2,C$1)+1,0)
Primary condition is to check whether any date mentioned on row 1 (within the month) is intersecting the either start or end date specified in cell A2
and B2
.主要条件是检查第 1 行(当月内)提到的任何日期是否与单元格
A2
和B2
中指定的开始日期或结束日期相交。 Once this is cleared then the primary logic which you had posted works just fine.清除此问题后,您发布的主要逻辑就可以正常工作。
You will have to change the argument separators as per your locale!您必须根据您的语言环境更改参数分隔符!
Edit -编辑-
@AnilGoyal spotted the formula error and I am posting the revised formula. @AnilGoyal 发现了公式错误,我发布了修改后的公式。 I have put those missing
AND
conditions in OR
.我已将那些缺失的
AND
条件放在OR
中。 In addition to that the formula would have missed a boundary condition where date was first or last day of the month.除此之外,该公式会错过日期是该月的第一天或最后一天的边界条件。
=IF(OR(AND(C$1>=$A2,C$1<=$B2),AND(EOMONTH(C$1,0)>=$A2,EOMONTH(C$1,0)<=$B2),AND($A2<=EOMONTH(C$1,0),$A2>=C$1),AND($B2<=EOMONTH(C$1,0),$B2>=C$1)),MIN($B2,EOMONTH(C$1,0))-MAX($A2,C$1)+1,0)
Overall, OP's original formula shall work well.总的来说,OP的原始公式应该很好用。 As it turns out below formula seems to work just fine as well.
事实证明,下面的公式似乎也可以正常工作。
=MAX(0,MIN($B2,EOMONTH(E$1,0))-MAX($A2,E$1)+1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.