[英]Calculate date overlap in months
Need a bit of help, if you'd be so kind.需要一点帮助,如果你这么好心的话。
In MS Excel, I'm trying to work out the number of months that a student attends university based off of the academic year.在 MS Excel 中,我试图根据学年计算出学生上大学的月数。
For instance, a student enrolls & finishes university on the following dates:例如,学生在以下日期注册并完成大学学业:
and there are multiple academic years which the student attends:学生参加多个学年:
01/08/2017 - 31/07/2018 2017 年 1 月 8 日 - 2018 年 7 月 31 日
01/08/2018 - 31/07/2019 01/08/2018 - 31/07/2019
01/08/2018 - 31/07/2020 2018 年 1 月 8 日 - 2020 年 7 月 31 日
I want to calculate how many months the student attends in each academic year:我想计算学生每个学年上学的月份:
01/08/2017 - 31/07/2018 (1 month) 2017 年 1 月 8 日 - 2018 年 7 月 31 日(1 个月)
01/08/2018 - 31/07/2019 (12 months) 2018 年 1 月 8 日 - 2019 年 7 月 31 日(12 个月)
01/08/2018 - 31/07/2020 (11 months) 2018 年 1 月 8 日 - 2020 年 7 月 31 日(11 个月)
I've managed to calculate the days using this function:我已经设法使用这个 function 计算了天数:
=MAX(MIN(term_end_date, student_,end_date) - MAX(term_start_date, student_start_date)+1,0)
But, I need months, if possible.但是,如果可能的话,我需要几个月的时间。
Appreciate the help in advance.提前感谢帮助。
=DATEDIF(A1,B1,”M”) will give the number of whole months between 2 dates (ie, excluding start and end months) where dates are entered in A1 and B1. =DATEDIF(A1,B1,”M”) 将给出在 A1 和 B1 中输入日期的两个日期(即不包括开始和结束月份)之间的整月数。 The “M” signifies that you'd like the information reported in months.
“M”表示您希望以月为单位报告信息。
For a more accurate result, try: =(DATEDIF(A1,B1,”D”) / 365) * 12. If the dates span multiple years - including leap years - try dividing by 365.25 vs 365.要获得更准确的结果,请尝试:=(DATEDIF(A1,B1,”D”) / 365) * 12。如果日期跨越多年 - 包括闰年 - 尝试除以 365.25 和 365。
Please try this UDF.请试试这个 UDF。
Function MonthsOverlap(Rng As Range, _
AccYear As Integer) As String
' 016
Const Ststart As Long = 1 ' index of array 'Study'
Const Stend As Long = 2 ' index of array 'Study'
Dim Fun As Integer ' Months of overlap
Dim YearStart As Date, YearEnd As Date
Dim Study As Variant
YearStart = DateSerial(AccYear, 8, 1) ' modify year and day as required
YearEnd = DateAdd("yyyy", 1, YearStart)
Study = Rng.Value ' Rng columns must be adjacent
With Application.WorksheetFunction
If Study(1, Ststart) <= YearStart Then
Fun = DateDiff("m", .Max(Study(1, Ststart), YearStart), .Min(Study(1, Stend), YearEnd))
ElseIf Study(1, Ststart) <= YearEnd Then
Fun = DateDiff("m", .Max(Study(1, Ststart), YearStart), YearEnd)
End If
Fun = .Max(Fun, 0)
End With
MonthsOverlap = Fun & " month" & IIf(Fun = 1, "", "s")
End Function
I tested it in a sheet like the one pictured below.我在一张如下图所示的表格中对其进行了测试。 Please observe the formula in the Formula Bar.
请注意公式栏中的公式。
As you see, the function returns unexpected results for the years 2017 and 2019. I don't think they are wrong.如您所见,function 在 2017 年和 2019 年返回了意想不到的结果。我认为它们没有错。 The formula just uses rounding of months different from your system.
该公式仅使用与您的系统不同的月份四舍五入。 I think the function can be modified to accommodate your rule once you make it known.
我认为一旦你知道 function 可以修改以适应你的规则。 Meanwhile it follows Microsoft's rule which, frankly, I also don't know.
同时它遵循微软的规则,坦率地说,我也不知道。 However, observe that the total time of study is 24 months which is correct and therefore sufficient, in conjunction with a look at the clock, to convince me to let good be good enough for today:-)
但是,请注意,学习的总时间是 24 个月,这是正确的,因此足以说服我让今天足够好:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.