我想创建一个 IIf 语句来计算孩子的预期离职日期。
例如,预期在31/08之前出生的孩子将在4岁后离开幼儿园,而在该日期之后出生的孩子将在5年之后离开。
现在我想做的是询问IIF声明,该声明会查看出生日期并决定要计算4年还是5年。但是,我一直在遇到我正在使用的代码的问题
= IIf([Date of Birth]>#31/08/0000# , =DateAdd("yyyy",4,[Date of Birth]) , =DateAdd("yyyy",5,[Date of Birth]))
因为有多个孩子的生日不同。应该有一种方法可以专门查看几个月。
编辑: 事实证明,这不是我老板所需要的,基本上他需要的是在孩子离开托儿所时(即,新学期到来且孩子4岁时)显示。如果孩子在9月之前出生,则可以在该年开始上学。如果他不是孩子,则该孩子适用于第二年的9月。 现在我不知道该怎么办,因为我执行IIF函数的尝试完全失败了。谁能帮忙?
答案 0 :(得分:0)
尝试:
=DateAdd("yyyy", IIf([Date of Birth] > DateSerial(Year([Date of Birth]), 8, 31), 4, 5), [Date of Birth])
编辑1 :
您可以像这样使用 DateAdd :
=IIf(DateAdd("yyyy", 4, [Date of Birth]) < DateSerial(Year(Date()), 9, 1), "Start school this year", "Postpone school start")
编辑2 :
或者您可以计算9月1日孩子的年龄:
AgeAtSeptember: Age([Date of Birth], DateSerial(Year(Date()), 9, 1))
使用此功能:
' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
' any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
ByVal DateOfBirth As Date, _
Optional ByVal AnotherDate As Variant) _
As Integer
Dim ThisDate As Date
Dim Years As Integer
If IsDateExt(AnotherDate) Then
ThisDate = CDate(AnotherDate)
Else
ThisDate = Date
End If
' Find difference in calendar years.
Years = DateDiff("yyyy", DateOfBirth, ThisDate)
If Years > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of DateOfBirth.
If DateDiff("d", ThisDate, DateAdd(IntervalSetting(DtInterval.dtYear), Years, DateOfBirth)) > 0 Then
Years = Years - 1
End If
ElseIf Years < 0 Then
Years = 0
End If
Age = Years
End Function