简体   繁体   English

excel 计算某个时期每月的天数

[英]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:我认为我的格式有问题,我不知道如何克服......通过更多测试,看起来我的公式一开始就很好:

  1. I create blank sheet and I manually add start dates - end dates and header column months to try formula, format it like date (with * as date - *14.3.20) and it worked fine... this is my screenshot:我创建空白表,然后手动添加开始日期 - 结束日期和 header 列月份来尝试公式,将其格式化为日期(使用 * 作为日期 - *14.3.20)并且效果很好......这是我的屏幕截图:

手动输入

  1. When I copy paste start dates and end dates from different sheet (I already have thousands of start-end dates... it would be impossible to manually enter all of them).当我从不同的工作表中复制粘贴开始日期和结束日期时(我已经有数千个开始日期......不可能手动输入所有日期)。 Than I paste special - just values, then in format I choose date, same format as my testing excel sheet... nothing it simply won't work... how I can format dates on pasted values?比我粘贴特殊的值 - 只是值,然后在格式中我选择日期,与我的测试 excel 表相同的格式......没有什么它根本行不通......我如何在粘贴的值上格式化日期? Here is screenshot that it is wrong...这是错误的屏幕截图...

错误的手动输入

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!谢谢你们的帮助!

ENTER THIS FORMULA IN C2在 C2 中输入这个公式

=IF(AND(C$1<MIN($A2,$B2),MONTH($A2)>MONTH(C$1)),0,IF(C$1>MAX($A2,$B2), 0,IF(C$1>=MIN(C$1,$A2),MIN(EOMONTH(C$1,0),$B2)-MAX($A2,C$1)+1,-MIN($B2,C$1)+$A2+1)))

在此处输入图像描述

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 行(当月内)提到的任何日期是否与单元格A2B2中指定的开始日期或结束日期相交。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM