Excel 一个月内两个日期之间有多少天?
使用Excel公式,在一个单元格中,我想计算多少天是Excel 一个月内两个日期之间有多少天?,excel,Excel,使用Excel公式,在一个单元格中,我想计算多少天是=startdate和我的方法: 使用数组公式创建从开始日期到开始日期+9999的日期数组。为此,它添加了0、1、2。。。从开始日期算起9999天,直到添加9999天为止。然后计算所需月份中的天数,并将结束日期降低或等于结束日期 E2中的公式为 =SUMPRODUCT((MONTH(A2+ROW($A$1:$A$10000)-1)=D2)*((A2+ROW($A$1:$A$10000)-1)<=B2)) =SUMPRODUCT((月(
=startdate
和我的方法:
使用数组公式创建从开始日期到开始日期+9999的日期数组。为此,它添加了0、1、2。。。从开始日期算起9999天,直到添加9999天为止。然后计算所需月份中的天数,并将结束日期降低或等于结束日期
E2
中的公式为
=SUMPRODUCT((MONTH(A2+ROW($A$1:$A$10000)-1)=D2)*((A2+ROW($A$1:$A$10000)-1)<=B2))
=SUMPRODUCT((月(A2+行($A$1:$A$10000)-1)=D2)*((A2+行($A$1:$A$10000)-1)我的方法:
使用数组公式创建从开始日期到startdate+9999的日期数组。为此,它会将0、1、2、…9999天添加到开始日期,直到添加9999天为止。然后计算所需月份中有多少天是在结束日期之前或之后
E2
中的公式为
=SUMPRODUCT((MONTH(A2+ROW($A$1:$A$10000)-1)=D2)*((A2+ROW($A$1:$A$10000)-1)<=B2))
=SUMPRODUCT((月(A2+行($A$1:$A$10000)-1)=D2)*((A2+行($A$1:$A$10000)-1)这最好通过使用自定义公式来实现。您需要进入VBA(右键单击图纸名称>编辑代码),插入模块(右键单击VBA面板>插入>模块中的图纸名称),然后粘贴此代码:
Function DaysInMonthBetween(startDate As Date, endDate As Date, chosenMonth As Integer)
Dim startYear, startMonth, startDay As Integer
Dim endYear, endMonth, endDay As Integer
Dim currentDate As Date
Dim sumDays, daysInMonth As Integer
sumDays = 0
startYear = Year(startDate)
startMonth = month(startDate)
startDay = Day(startDate)
endYear = Year(endDate)
endMonth = month(endDate)
endDay = Day(endDate)
If (startMonth > chosenMonth) Then startYear = startYear + 1
If (endMonth < chosenMonth) Then endYear = endYear - 1
If (endYear >= startYear) Then
For i = startYear To endYear
currentDate = DateSerial(i, chosenMonth, 1)
daysInMonth = Day(Application.WorksheetFunction.EoMonth(currentDate, 0))
sumDays = sumDays + daysInMonth
Next
If (startMonth = chosenMonth) Then sumDays = sumDays - (startDay - 1)
If (endMonth = chosenMonth) Then
currentDate = DateSerial(endYear, endMonth, 1)
daysInMonth = Day(Application.WorksheetFunction.EoMonth(currentDate, 0))
sumDays = sumDays - (daysInMonth - endDay)
End If
End If
DaysInMonthBetween = sumDays
End Function
函数DaysInMonthBetween(开始日期为日期,结束日期为日期,选择月份为整数)
Dim startYear、startMonth、startDay为整数
Dim endYear、endMonth、endDay为整数
将当前日期设置为日期
Dim sumDays,daysInMonth为整数
sumDays=0
startYear=年(起始日期)
startMonth=月(startDate)
startDay=天(startDate)
endYear=年份(endDate)
endMonth=月份(endDate)
endDay=天(endDate)
如果(startMonth>chosenMonth),则startYear=startYear+1
如果(endMonth=startYear),则
对于i=从开始到结束的年份
currentDate=DateSerial(i,选择月份,1)
daysInMonth=天(Application.WorksheetFunction.EoMonth(currentDate,0))
sumDays=sumDays+daysInMonth
下一个
如果(startMonth=chosenMonth),则sumDays=sumDays-(startDay-1)
如果(endMonth=chosenMonth),则
currentDate=DateSerial(endYear,endMonth,1)
daysInMonth=天(Application.WorksheetFunction.EoMonth(currentDate,0))
sumDays=sumDays-(daysInMonth-endDay)
如果结束
如果结束
DaysInMonthBetween=sumDays
端函数
然后,您可以通过键入=DaysInMonthBetween(startDate,endDate,chosenMonth)
在工作表中使用此自定义公式,因此,如果您的开始日期和结束日期分别为A1和A2,您可以键入=DaysInMonthBetween(A1,A2,1)
以获得1月份的结果,=DaysInMonthBetween(A1,A2,2)
用于二月等。这最好通过使用自定义公式来实现。您需要进入VBA(右键单击图纸名称>编辑代码),插入模块(右键单击VBA面板中的图纸名称>插入>模块)并粘贴此代码:
Function DaysInMonthBetween(startDate As Date, endDate As Date, chosenMonth As Integer)
Dim startYear, startMonth, startDay As Integer
Dim endYear, endMonth, endDay As Integer
Dim currentDate As Date
Dim sumDays, daysInMonth As Integer
sumDays = 0
startYear = Year(startDate)
startMonth = month(startDate)
startDay = Day(startDate)
endYear = Year(endDate)
endMonth = month(endDate)
endDay = Day(endDate)
If (startMonth > chosenMonth) Then startYear = startYear + 1
If (endMonth < chosenMonth) Then endYear = endYear - 1
If (endYear >= startYear) Then
For i = startYear To endYear
currentDate = DateSerial(i, chosenMonth, 1)
daysInMonth = Day(Application.WorksheetFunction.EoMonth(currentDate, 0))
sumDays = sumDays + daysInMonth
Next
If (startMonth = chosenMonth) Then sumDays = sumDays - (startDay - 1)
If (endMonth = chosenMonth) Then
currentDate = DateSerial(endYear, endMonth, 1)
daysInMonth = Day(Application.WorksheetFunction.EoMonth(currentDate, 0))
sumDays = sumDays - (daysInMonth - endDay)
End If
End If
DaysInMonthBetween = sumDays
End Function
函数DaysInMonthBetween(开始日期为日期,结束日期为日期,选择月份为整数)
Dim startYear、startMonth、startDay为整数
Dim endYear、endMonth、endDay为整数
将当前日期设置为日期
Dim sumDays,daysInMonth为整数
sumDays=0
startYear=年(起始日期)
startMonth=月(startDate)
startDay=天(startDate)
endYear=年份(endDate)
endMonth=月份(endDate)
endDay=天(endDate)
如果(startMonth>chosenMonth),则startYear=startYear+1
如果(endMonth=startYear),则
对于i=从开始到结束的年份
currentDate=DateSerial(i,选择月份,1)
daysInMonth=天(Application.WorksheetFunction.EoMonth(currentDate,0))
sumDays=sumDays+daysInMonth
下一个
如果(startMonth=chosenMonth),则sumDays=sumDays-(startDay-1)
如果(endMonth=chosenMonth),则
currentDate=DateSerial(endYear,endMonth,1)
daysInMonth=天(Application.WorksheetFunction.EoMonth(currentDate,0))
sumDays=sumDays-(daysInMonth-endDay)
如果结束
如果结束
DaysInMonthBetween=sumDays
端函数
然后,您可以通过键入=DaysInMonthBetween(startDate,endDate,chosenMonth)
在工作表中使用此自定义公式,因此,如果您的开始日期和结束日期分别为A1和A2,您可以键入=DaysInMonthBetween(A1,A2,1)
以获得1月份的结果,=DaysInMonthBetween(A1,A2,2)
用于二月等等。这是第一个答案的变体,使用间接生成开始日期和结束日期(F1)之间的日期数组:-
正如Axel Richter所提到的,您还可以使用索引生成日期数组(F2):-
使用这些公式的优点是,开始日期和结束日期之间的差异没有限制
使用“间接”的缺点是,它不稳定,在文件关闭时会生成“保存”提示
索引方法的缺点是容易插入/删除行
这是第一个答案的变体,使用间接生成开始日期和结束日期(F1)之间的日期数组:-
正如Axel Richter所提到的,您还可以使用索引生成日期数组(F2):-
使用这些公式的优点是,开始日期和结束日期之间的差异没有限制
使用“间接”的缺点是,它不稳定,在文件关闭时会生成“保存”提示
索引方法的缺点是容易插入/删除行
您是否也可以有很多年的间隔?例如,开始日期2009-01-15和结束日期2015-04-02?是的,间隔可以超过1年?您是否也可以有很多年的间隔?例如,开始日期2009-01-15和结束日期2015-04-02?是的,间隔可以超过1年。这是否需要一个数组公式?即使我不按s,它似乎也同样有效换档+控制+输入。否,此公式不需要用[Ct]完成