我需要与Microsoft Project持续时间相同的公式。需要在Excel和SharePoint任务中执行此操作。
因此该列应比较两个日期,如下所示:
1 wk
2 wks
3 days
1 day
1 mth
2 mths
答案 0 :(得分:1)
使用公式执行此操作并不是我愿意解决的问题,也许其他人感觉要处理无休止的嵌套if
混乱。
相反,相反,一个VBA例程将吐出两个日期之间的年,月,周和日数应该足够了。
Function projectDuration(fromdate As Date, todate As Date) As String
'years
years = DateDiff("yyyy", fromdate, todate)
If years > 1 Then
projectDuration = years & " yrs "
ElseIf years = 1 Then
projectDuration = years & " yr "
End If
'months
months = DateDiff("m", DateAdd("yyyy", years, fromdate), todate)
If months > 1 Then
projectDuration = projectDuration & months & " mths "
ElseIf months = 1 Then
projectDuration = projectDuration & months & " mth "
End If
'weeks
weeks = DateDiff("w", DateAdd("m", months, DateAdd("yyyy", years, fromdate)), todate)
If weeks > 1 Then
projectDuration = projectDuration & weeks & " wks "
ElseIf weeks = 1 Then
projectDuration = projectDuration & weeks & " wk "
End If
'days
days = DateDiff("w", DateAdd("w", weeks, DateAdd("m", months, DateAdd("yyyy", years, fromdate))), todate)
If days > 1 Then
projectDuration = projectDuration & days & " days"
ElseIf days = 1 Then
projectDuration = projectDuration & days & " day"
End If
End Function
这是一个UDF。只需将其粘贴在VBE中的新模块中,然后就可以在工作表上使用此公式。例如,如果表单中包含以下内容:
+---+----------+-----------+
| | A | B |
+---+----------+-----------+
| 1 | 1/1/2016 | 2/16/2016 |
| 2 | | |
+---+----------+-----------+
您可以使用公式:
=projectDuration(A1,B1)
它会吐出1 mth 2 wks 1 day
。